Vendor 'github.com/skarademir/naturalsort'

Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>
This commit is contained in:
Ahmet Alp Balkan 2015-03-17 23:30:15 -07:00
parent 344c7d771a
commit f2918929b0
5 changed files with 133 additions and 0 deletions

4
Godeps/Godeps.json generated
View File

@ -21,6 +21,10 @@
"ImportPath": "github.com/cenkalti/backoff",
"Rev": "9831e1e25c874e0a0601b6dc43641071414eec7a"
},
{
"ImportPath": "github.com/skarademir/naturalsort",
"Rev": "9688a08870fba63de22ec85d167598bb677820ee"
},
{
"ImportPath": "github.com/codegangsta/cli",
"Comment": "1.2.0-64-ge1712f3",

View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2015 skarademir
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,16 @@
# naturalsort
A simple natural sorter for Go Strings
##Usage
Implements the `sort.Interface`
called by `sort.Sort(NaturalSort([]string)`
###Example
`SampleStringArray := []string{"z24", "z2", "z15", "z1",
"z3", "z20", "z5", "z11",
"z 21", "z22"}
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)

View File

@ -0,0 +1,34 @@
package naturalsort
import (
"regexp"
"strconv"
"strings"
)
type NaturalSort []string
func (s NaturalSort) Len() int {
return len(s)
}
func (s NaturalSort) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s NaturalSort) Less(i, j int) bool {
r1 := regexp.MustCompilePOSIX(`^([^0-9]*)+|[0-9]+`)
spliti := r1.FindAllString(strings.Replace(s[i], " ", "", -1), -1)
splitj := r1.FindAllString(strings.Replace(s[j], " ", "", -1), -1)
for index := range spliti {
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
}
return spliti[index] < splitj[index]
}
}
return s[i] < s[j]
}

View File

@ -0,0 +1,58 @@
package naturalsort
import (
"reflect"
"sort"
"testing"
)
func TestSortValid(t *testing.T) {
cases := []struct {
data, expected []string
}{
{
nil,
nil,
},
{
[]string{},
[]string{},
},
{
[]string{"a"},
[]string{"a"},
},
{
[]string{"0"},
[]string{"0"},
},
{
[]string{"1", "2", "30", "22", "0", "00", "3"},
[]string{"0", "00", "1", "2", "3", "22", "30"},
},
{
[]string{"A1", "A0", "A21", "A11", "A111", "A2"},
[]string{"A0", "A1", "A2", "A11", "A21", "A111"},
},
{
[]string{"A1BA1", "A11AA1", "A2AB0", "B1AA1", "A1AA1"},
[]string{"A1AA1", "A1BA1", "A2AB0", "A11AA1", "B1AA1"},
},
}
for i, c := range cases {
sort.Sort(NaturalSort(c.data))
if !reflect.DeepEqual(c.data, c.expected) {
t.Fatalf("Wrong order in test case #%d.\nExpected=%v\nGot=%v", i, c.expected, c.data)
}
}
}
func BenchmarkSort(b *testing.B) {
var data = [...]string{"A1BA1", "A11AA1", "A2AB0", "B1AA1", "A1AA1"}
for ii := 0; ii < b.N; ii++ {
d := NaturalSort(data[:])
sort.Sort(d)
}
}