Reputation: 1270
I have a function for sorting a multi-dimensional array using JavaScript. It's working perfectly in both Firefox and Google Chrome, but not at all in IE. Is there some coding standard I'm not adhering to for cross browser compatibility?
function sortArray(arr, column, order) {
var asc = function sortAsc(a, b) {
return (a[column] > b[column]);
}
var desc = function sortDesc(a, b) {
return (a[column] < b[column]);
}
if (order=="desc")
{
return arr.sort(desc);
}
return arr.sort(asc);
}
An example of a call would be: "sortArray(employees, 'name', 'desc')"
Any thoughts on what might fix this in IE so that it doesn't keep returning the original array would be helpful. Any ideas? Thanks!
Upvotes: 0
Views: 534
Reputation: 700592
You are taking advantage of a non-standard way of implementing the comparison, so it only works in some browsers.
The comparison should return zero if the items are equal, and a positive or negative value when they are not:
function asc(a, b) {
return (a[column] == b[column] ? 0 : a[column] < b[column] ? -1 : 1);
}
function desc(a, b) {
return asc(b, a);
}
Upvotes: 1