Skip to content

In HyperFormula, you can sort data by reordering rows and columns.

To sort data in HyperFormula, you reorder rows (or columns), by providing your preferred permutation of row (or column) indexes.

The permutation array has the form [ newPositionForRow0, newPositionForRow1, newPositionForRow2, ... ]. The value at index i is the new position for the row that is currently at index i.

You can implement any sorting algorithm that returns such an array of row or column indexes.

To sort rows, use the isItPossibleToSetRowOrder and setRowOrder methods.

Choose your required permutation of row indexes.

For example, if you want to move the bottom row to the top of a 3-row sheet, set the order to [1, 2, 0] instead of [0, 1, 2]. This moves the row at index 0 to position 1, the row at index 1 to position 2, and the row at index 2 to position 0:

// a HyperFormula instance with example data
const hfInstance = HyperFormula.buildFromArray([
['A'],
['B'],
['C'],
], { licenseKey: 'gpl-v3' });
// we'll set the row order to [1, 2, 0] in the next steps
// the resulting sheet will be: [['C'], ['A'], ['B']]

Step 2: Check if the new row order can be applied

Section titled “Step 2: Check if the new row order can be applied”

Before you change the row order, check if your specified row number permutation can actually be applied.

Thanks to the isItPossibleTo* methods, you can check if an operation is allowed, and display an error message if it’s not.

Use the isItPossibleToSetRowOrder method:

const hfInstance = HyperFormula.buildFromArray([
['A'],
['B'],
['C'],
], { licenseKey: 'gpl-v3' });
// a variable to carry the user message
let messageUsedInUI;
// check if your permutation can be applied
const isRowOrderOk = hfInstance.isItPossibleToSetRowOrder(0, [1, 2, 0]);
// display an error message
if (!isRowOrderOk) {
messageUsedInUI = 'Sorry, you cannot sort rows in this way.'
}

If your specified row number permutation is valid, change the row order:

const hfInstance = HyperFormula.buildFromArray([
['A'],
['B'],
['C'],
], { licenseKey: 'gpl-v3' });
let messageUsedInUI;
const isRowOrderOk = hfInstance.isItPossibleToSetRowOrder(0, [1, 2, 0]);
if (!isRowOrderOk) {
messageUsedInUI = 'Sorry, you cannot sort rows in this way.'
} else {
// set the new row order
hfInstance.setRowOrder(0, [1, 2, 0]);
}
// the resulting sheet is: [['C'], ['A'], ['B']]
// the method returns an array of cells whose values changed:
// [{
// address: { sheet: 0, col: 0, row: 1 },
// newValue: 'A',
// },
// {
// address: { sheet: 0, col: 0, row: 2 },
// newValue: 'B',
// },
// {
// address: { sheet: 0, col: 0, row: 0 },
// newValue: 'C',
// }]

To sort columns, use the isItPossibleToSetColumnOrder and setColumnOrder methods.

The permutation array has the same shape as for rows: [ newPositionForColumn0, newPositionForColumn1, newPositionForColumn2, ... ]. The value at index i is the new position for the column that is currently at index i.

Choose your required permutation of column indexes.

For example, if you want to move the last column to the front of a 3-column sheet, set the order to [1, 2, 0] instead of [0, 1, 2]. This moves the column at index 0 to position 1, the column at index 1 to position 2, and the column at index 2 to position 0:

// a HyperFormula instance with example data
const hfInstance = HyperFormula.buildFromArray([
['A', 'B', 'C']
], { licenseKey: 'gpl-v3' });
// we'll set the column order to [1, 2, 0] in the next steps
// the resulting sheet will be: [['C', 'A', 'B']]

Step 2: Check if the new column order can be applied

Section titled “Step 2: Check if the new column order can be applied”

Before you change the column order, check if your specified column number permutation can actually be applied.

Thanks to the isItPossibleTo* methods, you can check if an operation is allowed, and display an error message if it’s not.

Use the isItPossibleToSetColumnOrder method:

const hfInstance = HyperFormula.buildFromArray([
['A', 'B', 'C']
], { licenseKey: 'gpl-v3' });
// a variable to carry the user message
let messageUsedInUI;
// check if your permutation can be applied
const isColumnOrderOk = hfInstance.isItPossibleToSetColumnOrder(0, [1, 2, 0]);
// display an error message
if (!isColumnOrderOk) {
messageUsedInUI = 'Sorry, you cannot sort columns in this way.'
}

If your specified column number permutation is valid, change the column order:

const hfInstance = HyperFormula.buildFromArray([
['A', 'B', 'C']
], { licenseKey: 'gpl-v3' });
let messageUsedInUI;
const isColumnOrderOk = hfInstance.isItPossibleToSetColumnOrder(0, [1, 2, 0]);
if (!isColumnOrderOk) {
messageUsedInUI = 'Sorry, you cannot sort columns in this way.'
} else {
// set the new column order
hfInstance.setColumnOrder(0, [1, 2, 0]);
}
// the resulting sheet is: [['C', 'A', 'B']]
// the method returns an array of cells whose values changed:
// [{
// address: { sheet: 0, col: 1, row: 0 },
// newValue: 'A',
// },
// {
// address: { sheet: 0, col: 2, row: 0 },
// newValue: 'B',
// },
// {
// address: { sheet: 0, col: 0, row: 0 },
// newValue: 'C',
// }]

The demo below shows how to sort rows in ascending and descending order, based on the results (calculated values) of the cells in the second column.

Name Score