mirror of https://github.com/docker/docs.git
Merge pull request #2399 from dgageot/update-dependencies
Update dependencies
This commit is contained in:
commit
f82ae1c252
|
@ -95,7 +95,7 @@
|
|||
},
|
||||
{
|
||||
"ImportPath": "github.com/skarademir/naturalsort",
|
||||
"Rev": "983d4d86054d80f91fd04dd62ec52c1d078ce403"
|
||||
"Rev": "69a5d87bef620f77ee8508db30c846b3b84b111e"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/smartystreets/go-aws-auth",
|
||||
|
|
|
@ -1,16 +1,22 @@
|
|||
# naturalsort
|
||||
A simple natural sorter for Go Strings
|
||||
A simple natural string sorter for Go.
|
||||
|
||||
##Usage
|
||||
Implements the `sort.Interface`
|
||||
|
||||
called by `sort.Sort(NaturalSort([]string)`
|
||||
called by `sort.Sort(NaturalSort([]string))`
|
||||
###Example
|
||||
`SampleStringArray := []string{"z24", "z2", "z15", "z1",
|
||||
|
||||
```go
|
||||
SampleStringArray := []string{
|
||||
"z24", "z2", "z15", "z1",
|
||||
"z3", "z20", "z5", "z11",
|
||||
"z 21", "z22"}
|
||||
sort.Sort(NaturalSort(SampleStringArray))`
|
||||
sort.Sort(NaturalSort(SampleStringArray))
|
||||
```
|
||||
|
||||
##Needless Description
|
||||
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).
|
||||
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
|
||||
|
|
|
@ -2,12 +2,13 @@ package naturalsort
|
|||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type NaturalSort []string
|
||||
|
||||
var r = regexp.MustCompile(`[^0-9]+|[0-9]+`)
|
||||
|
||||
func (s NaturalSort) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
|
@ -15,25 +16,45 @@ func (s NaturalSort) Swap(i, j int) {
|
|||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
func (s NaturalSort) Less(i, j int) bool {
|
||||
r := regexp.MustCompilePOSIX(`^([^0-9]*)+|[0-9]+`)
|
||||
|
||||
spliti := r.FindAllString(strings.Replace(s[i], " ", "", -1), -1)
|
||||
splitj := r.FindAllString(strings.Replace(s[j], " ", "", -1), -1)
|
||||
|
||||
splitshortest := len(spliti)
|
||||
if len(spliti) > len(splitj) {
|
||||
splitshortest = len(splitj)
|
||||
}
|
||||
for index := 0; index < splitshortest; index ++{
|
||||
for index := 0; index < len(spliti) && index < len(splitj); index++ {
|
||||
if spliti[index] != splitj[index] {
|
||||
inti, ei := strconv.Atoi(spliti[index])
|
||||
intj, ej := strconv.Atoi(splitj[index])
|
||||
if ei == nil && ej == nil { //if number
|
||||
return inti < intj
|
||||
// Both slices are numbers
|
||||
if isNumber(spliti[index][0]) && isNumber(splitj[index][0]) {
|
||||
// Remove Leading Zeroes
|
||||
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]
|
||||
}
|
||||
|
||||
}
|
||||
// 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]
|
||||
}
|
||||
func isNumber(input uint8) bool {
|
||||
return input >= '0' && input <= '9'
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue