Home > Back-end >  How to sort TWO columns in specific range by priorities?
How to sort TWO columns in specific range by priorities?

Time:07-26

I have a small desire with my football table...

I want to sort automatically ranges B3:J7, B11:J15, B19:J23, B27:J31, B35:J39, B43:J47, B51:J56 and B59:J64 by COLUMN K (Pts - Points) which is FIRST priority and when the teams are on equal points then by column J (GD - Goal Difference).

Is it possible?

I have a code in apps script but this sorts only by column K.

Thanks in advance!

Table link: https://docs.google.com/spreadsheets/d/17ZyUuDAp85Vr76NMmqQXuLqTUMMuHP6sQu2gvQREhDU/edit?usp=sharing

CodePudding user response:

Thank you!

Works like a charm! :)

CodePudding user response:

.sort() accepts multiple sort criteria, you can modify line 7 of your script for it to include an additional column:

ranges.forEach(r => r.sort([{ column: r.getLastColumn(), ascending: false }, { column: r.getLastColumn()-1, ascending: false }]));

This assumes that the range's last column is "K" and previous column to the left is "J", you would need to add the whole range when declaring the range, for example:

const a1Notations = ["B3:K7", "B11:K15", "B19:K23", "B27:K31", "B35:K39", "B43:K47", "B51:K56", "B59:K64"];

Reference:

  • Related