github.com/coreos/mantle@v0.13.0/lang/natsort/sort.go (about) 1 // Copyright 2016 CoreOS, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package natsort 16 17 import ( 18 "sort" 19 ) 20 21 // Less determines if a naturally comes before b. Unlike Compare it will 22 // fall back to a normal string comparison which considers spaces if the 23 // two would otherwise be equal. That helps ensure the stability of sorts. 24 func Less(a, b string) bool { 25 if r := Compare(a, b); r == 0 { 26 return a < b 27 } else { 28 return r < 0 29 } 30 } 31 32 // Strings natural sorts a slice of strings. 33 func Strings(s []string) { 34 sort.Slice(s, func(i, j int) bool { 35 return Less(s[i], s[j]) 36 }) 37 } 38 39 // StringsAreSorted tests whether a slice of strings is natural sorted. 40 func StringsAreSorted(s []string) bool { 41 return sort.SliceIsSorted(s, func(i, j int) bool { 42 return Less(s[i], s[j]) 43 }) 44 }