Sorting data | HyperFormula (v3.2.0)

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

# Sorting data in HyperFormula

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

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

# Sorting rows

To sort rows, use the isItPossibleToSetRowOrder and setRowOrder methods.

# Step 1: Choose a new row order

Choose your required permutation of row indexes.

For example, if you want to swap the first row with the third row, set the order to [2, 1, 0] instead of [0, 1, 2]:

TIP

The setRowOrder method accepts an array of numbers, so you can implement any function that returns an array with your required row order.

# 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:

# Step 3: Set the new row order

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

# Sorting columns

To sort columns, use the isItPossibleToSetColumnOrder and setColumnOrder methods.

# Step 1: Choose a new column order

Choose your required permutation of column indexes.

For example, if you want to swap the first column with the third column, set the order to [2, 1, 0] instead of [0, 1, 2]:

TIP

The setColumnOrder method accepts an array of numbers, so you can implement any function that returns an array with your required column order.

# 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:

# Step 3: Set the new column order

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

# Data sorting demo

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.