Home > Enterprise >  How to sort gsheet table according to a custom sort function?
How to sort gsheet table according to a custom sort function?

Time:09-22

I have a gsheet table with columns: version, valueA

I wrote an app-script function that compares two versions.

How can I use this custom cooperator to sort the whole table according to this comperator?

function sortAnalyticsVersionsDesc() {
  var versions = activeSpreadsheet.getRangeByName(MY_RANGE).getValues().filter(item => item[0] != "");
  versions.sort(_compareVer);
}

I want the valueA to be sorted accordingly.

sortSpecObj The columns to sort by gives no option to provide a custom sorting function.

Do I have to copy the whole table to memory? But even then - how do I sort a matrix?

CodePudding user response:

function sortAnalyticsVersionsDesc() {
  var lines = activeSpreadsheet.getRangeByName(ANALYTICS_DATA_TABLE).getValues().filter(item => item[0] != "");
  lines = lines.sort(_compareLinesWithVer);
  activeSpreadsheet.getRangeByName(ANALYTICS_DATA_TABLE).setValues(lines)
}

function _compareLinesWithVer(a, b) {
return _compareVer(a[2], b[2]);
}

function _compareVer(a, b) {
  • Related