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));

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.