1

In my Google sheet, I have 2 columns, A and B, which I would like to sort descending by values of Column B. This I know can be done using filter/sort options in sheets. This works as expected but this is a manual step that should be repeated every time data in column B changes.

I would like all rows to be automatically sorted when data in Column B changes. Any ideas to do this?

Example of data in column A and B

Team    Score
Team A  13
Team C  12
Team B  11
Team D  5
1
  • 1
    An onEdit script will enable you to identify when data in Column B changes, then use sheet.sort(columnPosition, ascending) (doc) will sort your sheet by column. Don't be put-off by the "ascending" reference, you can specify ascending or descending. Commented Apr 22, 2019 at 12:52

1 Answer 1

1

add this script to your sheet:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange("A1:Z");

function onEdit(e)  {
  range.sort([{column: 2, ascending: false}]);
}

  • Sheet1 = name of the sheet
  • A1:Z = range to be sorted
  • column: 2 = column B
  • ascending: false = descending
Sign up to request clarification or add additional context in comments.

3 Comments

I have an additional question. Lets say i need to sort the rows based on first the score and then the total number of goals. Like this:
I have an additional question. I need to sort the rows based on first the score (the solution you have) and then the total number of goals - lets say column 3 in this case. So if the score is equal I have to run a second sort that compare the number of goals . Any ideas ?
you can do it like this: i.sstatic.net/q66Sf.png or adding it one more time for different range separately

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.