Logo

Programming-Idioms

Sort the string list data in a case-insensitive manner.

The sorting must not destroy the original casing of the strings.
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
data.sort((a, b) => a.toUpperCase().compareTo(b.toUpperCase()));
import (
	"sort"
	"unicode"
	"unicode/utf8"
)
func lessCaseInsensitive(s, t string) bool {
	for {
		if len(t) == 0 {
			return false
		}
		if len(s) == 0 {
			return true
		}
		c, sizec := utf8.DecodeRuneInString(s)
		d, sized := utf8.DecodeRuneInString(t)

		lowerc := unicode.ToLower(c)
		lowerd := unicode.ToLower(d)

		if lowerc < lowerd {
			return true
		}
		if lowerc > lowerd {
			return false
		}

		s = s[sizec:]
		t = t[sized:]
	}
}

sort.Slice(data, func(i, j int) bool { return lessCaseInsensitive(data[i], data[j]) })
import (
	"sort"
	"strings"
)
sort.Slice(data, func(i, j int) bool {
	return strings.ToLower(data[i]) < strings.ToLower(data[j])
})
import "cmp"
import "slices"
slices.SortFunc(data, func(a, b string) int {
	return cmp.Compare(strings.ToLower(a), strings.ToLower(b))
})
data.sort(String::compareToIgnoreCase);
Classes;
data.sort;
@data = sort { lc($a) cmp lc($b) } @data;
data.sort(key=str.lower)
data.sort_by!(&:downcase)