SORTBY Function (LibreOffice Calc)
Sort LibreOffice Calc records by separate keys with SORTBY, including ascending defaults, mixed-direction tie breakers and horizontal sorting.
Verification status: Ten ordinary sorting component and dimension checks were recalculated in LibreOfficeDev 26.8.0.0.alpha0 (2c87e51eeaa2b413ff4ae097b2705eea1995d8e5) using headless XLSX import and forced recalculation. Exact identities, typed inputs/results and controls were checked. LibreOffice 26.8.1.1 was source-reviewed only; no stable-release or GUI array-entry execution is claimed. How formula examples are checked
Quick answer: =INDEX(SORTBY(H10:J13;I10:I13;1;J10:J13;-1);1;1) returned “B” in the tested development build.
Availability and version history
- Introduced: LibreOffice 24.8, explicitly stated in function-specific Help.
- Supported in: 26.8 Help and 26.8.1.1 registration; no retirement is established.
- Scope: Desktop Calc, English semicolon-separated formulas. The documented
COM.MICROSOFT.SORTBYnamespace is outside OpenFormula 1.3. - Checked: October 14, 2026. Ten listed examples were recalculated in LibreOfficeDev 26.8.0.0.alpha0 using headless XLSX import. Stable releases, 26.8.1.1 and GUI array entry were not executed.
What SORTBY returns
SORTBY returns a reordered copy of a range or array, using one or more corresponding key ranges. The keys can be outside the returned columns. A column-shaped key sorts records vertically; a row-shaped key sorts columns horizontally. The original cells are not rearranged. Function Help.
Syntax and arguments
SORTBY(Range; SortByRange1 [; SortOrder1 [; SortByRange2 [; SortOrder2 …]]])
Square brackets mark optional arguments; do not type them. Range is the data to return. Each SortByRange supplies one key per returned row or column. SortOrder is 1 for ascending or −1 for descending; the default is ascending. For multiple keys, supply explicit direction arguments so the key/direction pairs remain clear. Earlier keys have priority, and later keys break ties. The inspected callback accepts the two-argument single-key form even though the Help’s displayed syntax lists SortOrder1 without brackets.
Worksheet setup
Enter this table in H10:J13:
| Row | H: label | I: group | J: score |
|---|---|---|---|
| 10 | A | 2 | 10 |
| 11 | B | 1 | 40 |
| 12 | C | 2 | 30 |
| 13 | D | 1 | 20 |
For horizontal sorting, enter A, B, C, D across H15:K15 and 10, 40, 30, 20 across H16:K16.
Ten reproducible examples
The native column is from LibreOfficeDev 26.8.0.0.alpha0, build 2c87e51eeaa2b413ff4ae097b2705eea1995d8e5, using headless XLSX import and forced recalculation. Decimal values are rounded to twelve places; quoted text excludes the surrounding quotation marks from the returned value.
| Formula | Independent reference | LibreOfficeDev result |
|---|---|---|
=INDEX(SORTBY(H10:J13;I10:I13;1;J10:J13;-1);1;1) |
"B" |
"B" |
=INDEX(SORTBY(H10:J13;I10:I13;1;J10:J13;-1);4;1) |
"A" |
"A" |
=INDEX(SORTBY(H10:J13;J10:J13);2;1) |
"D" |
"D" |
=INDEX(SORTBY(H10:J13;J10:J13;-1);2;1) |
"C" |
"C" |
=ROWS(SORTBY(H10:J13;J10:J13)) |
4 |
4 |
=COLUMNS(SORTBY(H10:J13;J10:J13)) |
3 |
3 |
=INDEX(SORTBY(H15:K16;H16:K16;1);1;1) |
"A" |
"A" |
=INDEX(SORTBY(H15:K16;H16:K16;1);1;3) |
"C" |
"C" |
=INDEX(SORTBY(H10:J13;J10:J13;1);4;3) |
40 |
40 |
=INDEX(SORTBY(H15:K16;H16:K16;-1);2;1) |
40 |
40 |
Practical worksheet
=SORTBY(H10:J13;I10:I13;1;J10:J13;-1) sorts the group ascending, then the score descending within each group. The labels become B, D, C, A: group 1 comes before group 2, and the larger score comes first inside each group. Sorting only by descending score would instead produce B, C, D, A. A secondary key therefore changes the meaning of the ranking rather than simply refining its appearance.
Advanced interpretation and checks
A key does not have to be included in the returned data. The same four scores can order the label-only range H10:H13 while keeping the score column out of the result. Shape must still agree: a vertical four-entry key describes four data rows, not four arbitrary cells. In the horizontal example, the row of scores orders the columns, giving labels A, D, C, B for ascending scores. SORTBY preserves the full returned width or height; it does not discard fields that were not used as keys. Inspected callback.
Boundaries and troubleshooting
A key must be one-dimensional and match the relevant data dimension. The source checks these requirements and rejects direction values outside 1 and −1 after integer conversion. Invalid sizes and unusual fractional directions were not executed. No header is detected automatically: exclude header cells from both data and key ranges. The examples use numeric keys and distinct complete key combinations. Locale-dependent text collation and the order of rows tied on every key are not certified here.
Related functions
Official evidence and checking scope
The saved formula identities, local input values and typed results were reconciled. The stale SUM cache changed to 3 and the uncached SUM returned 7. Saved formulas use COM.MICROSOFT.SORTBY for this operation. The examples establish only the listed development-build results; unexecuted guards and current-source behavior remain separately scoped.