Functions > Vector and Matrix > Other Array Functions > Example: Sorting Functions
  
Example: Sorting Functions
Use the sort, reverse, csort, and rsort functions for arranging elements of an array in ascending or descending order. You may need to sort arrays for the interpolation and regression functions that require sorted inputs.
1. Define a vector to be sorted.
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Sort vector x in ascending order.
Click to copy this expression
3. Sort vector x in descending order.
Click to copy this expression
4. Define a data set.
Click to copy this expression
5. Use the csort function to sort the values of the second column of M in ascending order, while maintaining the relationship between the entries in the first and second columns.
Click to copy this expression
6. Use sorting to visually examining your data before further analysis, for example, to decide how many bins to request on a histogram.
Click to copy this expression
Click to copy this expression
7. Plot column 1 of sunhist versus column 0.
Click to copy this expression
8. Create a 3 column matrix by augmenting M with matrix A.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
9. Use the rsort function to rearrange the columns of R so that the specified row is in ascending order, while maintaining the relationship between the entries in all columns.
Click to copy this expression
Click to copy this expression
Click to copy this expression
The top 10 rows are already in ascending order, thus the result of rsort (the order of the columns) is identical for rows 0-9.
Row 10 (6, 3, 1710) is the first row that needs to be rearranged in ascending order, thus the order of the columns is changed accordingly to have (3, 6, 1710) in row 10.