FANDOM


By default, the Array.sort() method sorts strings case sensitively, so "B" will appear before "a" in the sorted array. This code snippit shows how to sort an Array of strings case insensitively.


arr = new Array('Dog', 'cat', 'Mouse', 'frog', 'Turtle');

function sortCaseInsensitive(a, b) {
	if (a.toLowerCase() < b.toLowerCase()) return -1;
	if (a.toLowerCase() > b.toLowerCase()) return 1;
	return 0;
}

//Case sensitive - returns "Dog, Mouse, Turtle, cat, frog"
print(arr.sort()); 

//Case insensitive - returns "cat, Dog, frog, Mouse, Turtle"
print(arr.sort(sortCaseInsensitive));