I am using jquery to extract text of a set of elements into an array and want to sort them after inserting an element into the array. However, the sort is not working (as in the array remains in the same order after the sort). What's wrong? Code excerpt is below:
var sortedList = [];
$("div.resource").each(function(i, item) {
var resource = $(this).html().toLowerCase();
sortedList.push(resource);
})
// Add the new item
sortedList.push(resource_name.toLowerCase());
alert("before sort");
for (var i = 0; i < sortedList.length; i++) {
alert(sortedList[i]);
}
// Sort the list
sorted = sortedList.sort();
alert("after sort");
for (var i = 0; i < sorted.length; i++) {
alert(sorted[i]);
}
console.logthe array... (console.log(sorted);)var array = ["test", "fry", "aba"];..html()so you might be getting HTML tags in there (if the elements have children). Try using.text()instead. Also, try to$.trim()the strings.