github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/util/sorting/alphabetic.go (about)

     1  package sorting
     2  
     3  import "unicode"
     4  
     5  // Alphabetic is an array of strings that can be alphabetically sorted
     6  type Alphabetic []string
     7  
     8  func (s Alphabetic) Len() int      { return len(s) }
     9  func (s Alphabetic) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    10  
    11  func (s Alphabetic) Less(i, j int) bool {
    12  	return SortAlphabetic(s[i], s[j])
    13  }
    14  
    15  // SortAlphabetic will return true if string a comes after string b
    16  func SortAlphabetic(a string, b string) bool {
    17  	iRunes := []rune(a)
    18  	jRunes := []rune(b)
    19  
    20  	max := len(iRunes)
    21  	if max > len(jRunes) {
    22  		max = len(jRunes)
    23  	}
    24  
    25  	for idx := 0; idx < max; idx++ {
    26  		ir := iRunes[idx]
    27  		jr := jRunes[idx]
    28  
    29  		lir := unicode.ToLower(ir)
    30  		ljr := unicode.ToLower(jr)
    31  
    32  		if lir != ljr {
    33  			return lir < ljr
    34  		}
    35  
    36  		// the lowercase runes are the same, so compare the original
    37  		if ir != jr {
    38  			return ir < jr
    39  		}
    40  	}
    41  
    42  	return false
    43  }