ajSort function
Description
The ajSort function sorts the contents of a range or array.
Syntax
ajSort(in_array, [sort_index], [sort_order], [by_col], [convert_to_text])
Argument Name | Argument Type | Description |
---|---|---|
in_array (required) | Range / Array | The range of cells to be sorted. |
sort_index (optional) | Double | Points to a specified column or row (depending on the value set in ‘By_col’) to be sorted. If you do not specify anything or the number exceed the in_array range, the default value will be 1. |
sort_order (optional) | Double | 1 indicates sorting A to Z. 2 indicates sorting Z to A The default value is 1. |
by_col (optional) | Boolean | If it equals FALSE, the rows will be sorted according to the column index specified. If it equals TRUE, the columns will be sorted according to the row index specified. The default value is FALSE. All values other than 0 and FALSE will be treated as TRUE. |
convert_to_text (optional) | Boolean | If it equals TRUE, the return result will be converted to string values. If it equals FALSE, the return result will preserve the original data type. The default value is FALSE. |
The function will return:
1) Return Value: The sorted range of cells
2) Return Type: Multiple values (array formula)
Examples
Here are a few examples of ajSort function.
Example 1 (sort according to column index)
The function in this example sorts the range B2:E6 by the second column in ascending order.
As ‘convert_to_text’ is FALSE, the values in column Header2 and column Header4 are treated as numbers. Thus, their column sums are calculated correctly.
Example 2 (sort according to row index)
This is an example of sort by column. This function in this example sorts the range C22:F25 by the second row in descending order.
As ‘convert_to_text’ is TRUE, all values are treated as strings. Therefore, all row sums are 0.
Click here to download the use case workbooks for further reference.
Error Scenarios
It will return #VALUE! when missing any required parameter or mismatch parameter type. Besides, system will raise error for below scenario(s).
Error Scenario |
---|
Sort_index column contains some excel error. |
Invalid sort index, it should be a number. |
Invalid sort order, it should be 1 or 2. |