I have an Array of objects, each of which has the properties “first”, “last” and “number”.
I am attempting to sort this array by extending array sorting, and need to be able to sort by any of the properties. The numbers are saved as strings upon object instantiation. Below is the custom sort function I am trying, but it is not behaving well. If I sort by “last”, then output the three properties by line, I am showing it working OK through about the B’s, then it switches to reverse order, starting with Z’s down to C’s. If I sort by number, then I get 1-8 correct, then it lists 20, then 19, 18, etc. down to 9. Any thoughts on how to fix/improve would be appreciated. I have verified that the array is being created appropriately by pushing new objects and outputs to a textFrame.contents correctly before I do the sort.
function Obj(last, first, number) {
this.last = last;
this.first = first;
this.table = number;
};
var sort = function (arr, sortOption) {
arr.sort(function (a, b) {
if (sortOption == “number”) {
a[sortOption] = parseInt(a[sortOption], 10);
b[sortOption] = parseInt(b[sortOption], 10);
}
if (a[sortOption] < b[sortOption]) {
return -1;
} else if (a[sortOption] > b[sortOption]) {
return 1;
} else {
return 0;
}
});
};
sort(arr, “last”);