github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/util/sorting/alphabetic.go (about)

     1  package sorting
     2  
     3  import (
     4  	"unicode"
     5  )
     6  
     7  type AlphabetSorter func([]string) func(i, j int) bool
     8  
     9  // LessIgnoreCase returns true if first
    10  func LessIgnoreCase(first string, second string) bool {
    11  	iRunes := []rune(first)
    12  	jRunes := []rune(second)
    13  
    14  	max := len(iRunes)
    15  	if max > len(jRunes) {
    16  		max = len(jRunes)
    17  	}
    18  
    19  	for idx := 0; idx < max; idx++ {
    20  		ir := iRunes[idx]
    21  		jr := jRunes[idx]
    22  
    23  		lir := unicode.ToLower(ir)
    24  		ljr := unicode.ToLower(jr)
    25  
    26  		if lir != ljr {
    27  			return lir < ljr
    28  		}
    29  
    30  		// the lowercase runes are the same, so compare the original
    31  		if ir != jr {
    32  			return ir < jr
    33  		}
    34  	}
    35  
    36  	return false
    37  }
    38  
    39  // SortAlphabeticFunc returns a `less()` comparator for sorting strings while
    40  // respecting case.
    41  func SortAlphabeticFunc(list []string) func(i, j int) bool {
    42  	return func(i, j int) bool {
    43  		return LessIgnoreCase(list[i], list[j])
    44  	}
    45  }