Reputation: 1591
I have the following fiddle that is my attempt at
My issue is that I want to be able to make the sort NOT take into consideration the case of the value when sorting... Im noticing that it considers the case and puts uppercase values FIRST.
Im fairly certain its a matter of integrating .toLowerCase() somewhere but all my attempts have failed. Where / how do I apply .toLowerCase() to make the sort case insensitive?
function sortResults(prop, asc) {
//SORT THE ARRAY BY THE PASSED NODE VALUE...
myArray.jsonData = myArray.jsonData.sort(function(a, b) {
if (asc) return (a[prop]> b[prop]) ? 1 : ((a[prop] < b[prop]) ? -1 : 0);
else return (b[prop] > a[prop]) ? 1 : ((b[prop] < a[prop]) ? -1 : 0);
});
showInDOM();
}
Upvotes: 1
Views: 1532
Reputation: 62585
Use the function toLowerCase() and convert to string with + ""
myArray.jsonData.sort(function(a, b) {
var comp = ((a[prop]+"").toLowerCase() >
(b[prop]+"").toLowerCase()) ? 1 : -1 ;
return asc ? comp : -comp;
});
Upvotes: 2
Reputation: 17357
If you are always sorting strings, you can use:
function(a,b){
return a.toLowerCase() > b.toLowerCase();
}
which in your example would be:
myArray.jsonData.sort(function(a,b){
var result,
av=a[prop].toLowerCase(),
bv=b[prop].toLowerCase();
return asc ? av > bv : av < bv;
}
Upvotes: -1