Home > Net >  How to sort google-sheet table according to version number column
How to sort google-sheet table according to version number column

Time:09-22

I have a gsheet table with columns: version, valueA

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

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

I want the valueA to be sorted accordingly.

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

From the documentation it seems not possible.

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:

I should take the whole table and re-write it.

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