There is a way to do it, but it's a bit cumbersome. I've prepared the folowing data in the DATA sheet:
In another sheet (SORT) you can apply the SORT
function:
=SORT(DATA!A3:J40,DATA!A1,DATA!A2,DATA!B1,DATA!B2,DATA!C1,DATA!C2,DATA!D1,DATA!D2,DATA!E1,DATA!E2,DATA!F1,DATA!F2,DATA!G1,DATA!G2,DATA!H1,DATA!H2,DATA!I1,DATA!I2,DATA!J1,DATA!J2)
This will yield the following:
Now you will be able to change the sort by changing the second row (sheet DATA) from 0
to 1
. You can even change the column numbering. The SORT
function allows for up to 30 different sorting options. You need a very big pool of data, in order for it to be effective.
See example file I created: Sort With Many Columns
You can add a sorting column without having it appear on the sheet, within the query command. Example:
=query({A2:AB, arrayformula(hlookup(E2:E, {"str1","str2","str3",""; 1,2,3,4}, 2, false))}, "select Col3, Col1, Col2 where Col2 = 'New York' order by Col4")
Explanation
arrayformula(hlookup(E2:E, {"str1","str2","str3",""; 1,2,3,4}, 2, false))
creates a column with the order you want. This column gets appended to the queried array using array notation:
{A2:AB, arrayformula(...)}
This new array can now be queried. However, since it's a new array and not something placed in the sheet, one cannot use letters A,B,C... to refer to columns. Instead, one has to write Col1, Col2, etc:
"select Col3, Col1, Col2 where Col2 = 'New York' order by Col4"
Error handling
What if some values in the table are not listed in your order, e.g., null values? Add iferror
clause to sort them to the bottom:
arrayformula(iferror(hlookup(E2:E, {"str1","str2","str3",""; 1,2,3,4}, 2, false), 1000)
This gives the sorting order 1000 to the values not listed.
Helper column alternative
If having to switch to Col1 notation is too much of a price to pay, add the helper column
=arrayformula(hlookup(E2:E, {"str1","str2","str3",""; 1,2,3,4}, 2, false))
to the sheet; it would be column AC for you. It can be made hidden so it doesn't get in the way. Then query normally and sort by AC.
Lazy alternative
If there are not too many options, you can just repeat the command and join the results using array notation:
= {query(..., "... E='string1'"); query(..., "... E='string2'"); ...}
Like this:
={ QUERY('Raw Data'!A2:AB,"SELECT E, A, C, D, Y, R, S WHERE S = 'New York' AND E = 'string1'"); QUERY('Raw Data'!A2:AB,"SELECT E, A, C, D, Y, R, S WHERE S = 'New York' AND E = 'string2'") }
You'll need to avoid header rows when doing this, because they would be repeated.
Best Answer
If you want to create a new range, which is a sorted view of the input range, you would use
=SORT
. The=SORT
function can take multiple columns as parameters:In your case, you want to sort by column
B
in descending order, then by columnC
in ascending order. Given you have your data in columnA
throughC
, this should do what you want:If you want to output the rank of a team in the sorted result set, the easiest way is to use the row number,
=ROW
.So
=ROW(H2)
would yield2
. Knowing that, we could say that the 1-based rank of a row is=ROW(any row in the result) - 1
. If we have the sorted result table inH2:J
, we can output the rank of each row:I set up an example spreadsheet to demonstrate, feel free to take a look and/or copy it.
And check the documentation for
=SORT
and=ROW
.If you want to sort the input range in-place, you can't use a formula - because a formula needs an input range and an output range, which must be different.
For a one-time sort, select the input data (including the header row) and click Tools → Sort range. The dialog that appears has a checkbox for Data has header row - click it.
Specify a sort column (Rank 1), and descending order (Z→A). Then click Add another sort column, and select Rank 2, and ascending (A→Z).
This will sort the range - but only once, so if you alter the data, you will need to repeat the sort procedure.
The third option is to create a Filter view. Select the data, and click _Data → Filter views → Create new Filter view.
Give the filter a name. Then, for each column you want to use in your sort, click the down arrow icon (2). Specify the sort order for each column. Close the filter view by clicking the X in the upper right corner.
This will bring you back to your input data (unsorted). To go back to the sorted view, click _Data → Filter views → [your filter view].
Also in this case, you need to repeat the sort procedure when data changes.