The problem I face is that my array is not indexed by 0,1,2,3 etc. Its indexes are an id 0654, 3423, 7543 etc... so for example:
var obj = {};
obj["0654"] = { "Name" : "Tony" };
obj["3423"] = { "Name" : "Fred" };
obj["7543"] = { "Name" : "Dave" };
I am looking for a way to sort these by the Name key. But I keep getting an not a function error. Not too sure what I am doing wrong. I am using this code to sort it:
Array.prototype.sortByProp = function(p){
return this.sort(function(a,b){
return (a[p].toLowerCase() > b[p].toLowerCase()) ? 1 : (a[p].toLowerCase() < b[p].toLowerCase()) ? -1 : 0;
});
}
Doing this:
obj.sortByProp("Name");
Any guidance or the best way to approach this would be greatly appreciated... If this is a duplicate from another post, my apologies please point me in the right direction because I searched for this scenario all over and could not find the solution? Also, if you could tell me why this doesn't work too I can then understand objects a little better.