github.com/orange-cloudfoundry/cli@v7.1.0+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 is alphabetically less than second.
    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  			continue
    28  		}
    29  
    30  		return lir < ljr
    31  	}
    32  
    33  	return false
    34  }
    35  
    36  // SortAlphabeticFunc returns a `less()` comparator for sorting strings while
    37  // respecting case.
    38  func SortAlphabeticFunc(list []string) func(i, j int) bool {
    39  	return func(i, j int) bool {
    40  		return LessIgnoreCase(list[i], list[j])
    41  	}
    42  }