I have a list of data that I'm displaying in a graph, and I want the data to be displayed alphabetically. When the data points are all words (e.g. "white", "asian", "african american") my sorting function works correctly, but when my data points include numbers (e.g. "1", "10", "<1") the sorting doesn't work the way I need it to. This is my sorting script:
var series = response.series;
series = series.sort(function(a, b) {
var textA = a.name.toLowerCase();
var textB = b.name.toLowerCase();
console.log("Comparing " + textA + " to " + textB);
var returnVal = (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
console.log(returnVal);
return returnVal;
});
I can see from the console logging that the comparisons appear to be comparing correctly, but when I check series again after the sorting, it's the same as before. This is a larger example of my data:
series = [
{name: "1"}
{name: "10"}
{name: "11"}
{name: "12"}
{name: "13"}
{name: "2"}
{name: "3"}
{name: "4"}
{name: "5"}
{name: "6"}
{name: "7"}
{name: "8"}
{name: "9"}
{name: "<1"}
{name: "Total 18 and below"}
]