How Do I Pre-sort Numbers And Not Screw Up The Formatting?
I have a custom function I am writing that will return an array of arrays: function tester(col){ var rows = [ // this gives me an error on sorting, so turn numbers to strings
Solution 1:
UPDATE
Since you want to sort by number if they are numbers, and by string if they are strings, you can do:
function tester(col){
var rows = [
[4,2,3,"Tom"],
[0,8,9,"Bill"],
[5,7,1,"Bob"],
[1,2,3,"Charlie"]
];
rows.sort(function(a, b) {
if (typeof a[col] === 'number')
return a[col] > b[col];
return a[col].localeCompare(b[col]);
});
return rows;
}
Post a Comment for "How Do I Pre-sort Numbers And Not Screw Up The Formatting?"