Building upon the contributions of charles-clayton and @vikas-gautam, I introduced the string test required for columns containing strings as in the original post.
return isNaN(a-b) ? (a === b) ? 0 : (a < b) ? -1 : 1 : a-b ;
The isNaN(a-b)
test checks if the strings cannot be converted to numbers. If they can, then the comparison a-b
is valid.
It's important to note that sorting a column with mixed data types will always lead to interesting outcomes since the direct equality comparison (a === b)
will consistently result in false.
You can find the complete script with Logger test below, utilizing Google Apps Script for implementation.
function testSort(){
function sortByCol(arr, colIndex){
arr.sort(sortFunction);
function sortFunction(a, b) {
a = a[colIndex];
b = b[colIndex];
return isNaN(a-b) ? (a === b) ? 0 : (a < b) ? -1 : 1 : a-b ; // check for text strings that cannot be converted to numbers
}
}
// Usage
var a = [ [12,'12', 'AAA'],
[12,'11', 'AAB'],
[58,'120', 'CCC'],
[28,'08', 'BBB'],
[18,'80', 'DDD'],
]
var arr1 = a.map(function (i){return i;}).sort(); // use map to avoid interference from in-place sorting.
Logger.log("Original unsorted:\n " + JSON.stringify(a));
Logger.log("Vanilla sort:\n " + JSON.stringify(arr1));
sortByCol(a, 0);
Logger.log("By col 0:\n " + JSON.stringify(a));
sortByCol(a, 1);
Logger.log("By col 1:\n " + JSON.stringify(a));
sortByCol(a, 2);
Logger.log("By col 2:\n " + JSON.stringify(a));
/* vanilla sort returns " [
[12,"11","AAB"],
[12,"12","AAA"],
[18,"80","DDD"],
[28,"08","BBB"],
[58,"120","CCC"]
]
if col 0 then returns "[
[12,'12',"AAA"],
[12,'11', 'AAB'],
[18,'80',"DDD"],
[28,'08',"BBB"],
[58,'120',"CCC"]
]"
if col 1 then returns "[
[28,'08',"BBB"],
[12,'11', 'AAB'],
[12,'12',"AAA"],
[18,'80',"DDD"],
[58,'120',"CCC"],
]"
if col 2 then returns "[
[12,'12',"AAA"],
[12,'11', 'AAB'],
[28,'08',"BBB"],
[58,'120',"CCC"],
[18,'80',"DDD"],
]"
*/
}