Merge pull request #2399 from dgageot/update-dependencies

Update dependencies
This commit is contained in:
David Gageot 2015-11-24 16:07:02 +01:00
commit f82ae1c252
3 changed files with 45 additions and 18 deletions

2
Godeps/Godeps.json generated
View File

@ -95,7 +95,7 @@
}, },
{ {
"ImportPath": "github.com/skarademir/naturalsort", "ImportPath": "github.com/skarademir/naturalsort",
"Rev": "983d4d86054d80f91fd04dd62ec52c1d078ce403" "Rev": "69a5d87bef620f77ee8508db30c846b3b84b111e"
}, },
{ {
"ImportPath": "github.com/smartystreets/go-aws-auth", "ImportPath": "github.com/smartystreets/go-aws-auth",

View File

@ -1,16 +1,22 @@
# naturalsort # naturalsort
A simple natural sorter for Go Strings A simple natural string sorter for Go.
##Usage ##Usage
Implements the `sort.Interface` Implements the `sort.Interface`
called by `sort.Sort(NaturalSort([]string)` called by `sort.Sort(NaturalSort([]string))`
###Example ###Example
`SampleStringArray := []string{"z24", "z2", "z15", "z1",
```go
SampleStringArray := []string{
"z24", "z2", "z15", "z1",
"z3", "z20", "z5", "z11", "z3", "z20", "z5", "z11",
"z 21", "z22"} "z 21", "z22"}
sort.Sort(NaturalSort(SampleStringArray))` sort.Sort(NaturalSort(SampleStringArray))
```
##Needless Description ##Needless Description
Inspired by [Jeff Atwood's seminal blog post](http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/) and Inspired by [Jeff Atwood's seminal blog post](http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/) and
structured similarly to [Ian Griffiths' C# implementation](http://www.interact-sw.co.uk/iangblog/2007/12/13/natural-sorting). structured similarly to [Ian Griffiths' C# implementation](http://www.interact-sw.co.uk/iangblog/2007/12/13/natural-sorting).
This uses a regex to split the numeric and non-numeric portions of the string into a chunky array. Next, the left and right sides' This uses a regex to split the numeric and non-numeric portions of the string into a chunky array. Next, the left and right sides'
chunks are compared either by string comparrison (if either is a non-numeric chunk), or by integer (if both chunks are numeric) chunks are compared either by string comparrison (if either chunk is a non-numeric), or by ~~integer (if both chunks are numeric)~~ a character-by-character iterative function that compares numerical strings

View File

@ -2,12 +2,13 @@ package naturalsort
import ( import (
"regexp" "regexp"
"strconv"
"strings" "strings"
) )
type NaturalSort []string type NaturalSort []string
var r = regexp.MustCompile(`[^0-9]+|[0-9]+`)
func (s NaturalSort) Len() int { func (s NaturalSort) Len() int {
return len(s) return len(s)
} }
@ -15,25 +16,45 @@ func (s NaturalSort) Swap(i, j int) {
s[i], s[j] = s[j], s[i] s[i], s[j] = s[j], s[i]
} }
func (s NaturalSort) Less(i, j int) bool { func (s NaturalSort) Less(i, j int) bool {
r := regexp.MustCompilePOSIX(`^([^0-9]*)+|[0-9]+`)
spliti := r.FindAllString(strings.Replace(s[i], " ", "", -1), -1) spliti := r.FindAllString(strings.Replace(s[i], " ", "", -1), -1)
splitj := r.FindAllString(strings.Replace(s[j], " ", "", -1), -1) splitj := r.FindAllString(strings.Replace(s[j], " ", "", -1), -1)
splitshortest := len(spliti) for index := 0; index < len(spliti) && index < len(splitj); index++ {
if len(spliti) > len(splitj) {
splitshortest = len(splitj)
}
for index := 0; index < splitshortest; index ++{
if spliti[index] != splitj[index] { if spliti[index] != splitj[index] {
inti, ei := strconv.Atoi(spliti[index]) // Both slices are numbers
intj, ej := strconv.Atoi(splitj[index]) if isNumber(spliti[index][0]) && isNumber(splitj[index][0]) {
if ei == nil && ej == nil { //if number // Remove Leading Zeroes
return inti < intj stringi := strings.TrimLeft(spliti[index], "0")
stringj := strings.TrimLeft(splitj[index], "0")
if len(stringi) == len(stringj) {
for indexchar := 0; indexchar < len(stringi); indexchar++ {
if stringi[indexchar] != stringj[indexchar] {
return stringi[indexchar] < stringj[indexchar]
}
}
return len(spliti[index]) < len(splitj[index])
}
return len(stringi) < len(stringj)
} }
// One of the slices is a number (we give precedence to numbers regardless of ASCII table position)
if isNumber(spliti[index][0]) || isNumber(splitj[index][0]) {
return isNumber(spliti[index][0])
}
// Both slices are not numbers
return spliti[index] < splitj[index] return spliti[index] < splitj[index]
} }
} }
// Fall back for cases where space characters have been annihliated by the replacment call
// Here we iterate over the unmolsested string and prioritize numbers over
for index := 0; index < len(s[i]) && index < len(s[j]); index++ {
if isNumber(s[i][index]) || isNumber(s[j][index]) {
return isNumber(s[i][index])
}
}
return s[i] < s[j] return s[i] < s[j]
} }
func isNumber(input uint8) bool {
return input >= '0' && input <= '9'
}