github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/util/sorting/alphabetic.go (about)

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