Spread for WPF supports sorting data within the worksheet. You can use this feature to organize data efficiently and analyze data quickly.
Sorting is performed based on the data type of the cell values. For instance, when sorting cells with the DateTime data type, the data is arranged by date. Similarly, cells with string data are sorted in alphabetical order.
You can sort data in one or more columns within a worksheet using the Apply method of the ISort interface. The sort order can be specified as either in ascending (A to Z, 0 to 9) or descending (Z to A, 9 to 0) order using the SortOrder enumeration.
You can define the sort fields using the Add method and clear all of them using the Clear method of the ISortFields interface
Sorting can be performed based on cell values (e.g., text, numbers), font color, cell color, or conditional formatting icons. To set these criteria, use the SortOn property of the ISortField interface. Additionally, custom sorting can also be applied to the data. For example, use the TextAsNumbers field of the SortDataOption enumeration to sort the text as a number.
The following image illustrates the 'Name' column sorted in ascending order.
Refer to the following example code to sort the 'Name' column in ascending order.
You can use the SetRange method of the ISort interface to sort the data within a specified range of cell, rows, and elements. By specifying the range, you can narrow down the scope of the sorting action to focus on specific portions of data, rather than applying it to the entire dataset.
The following GIF illustrates the ‘Quantity’ column being sorted for the first 10 products in ascending order.
Refer to the following example code to apply sort to a particular cell range of the ‘Quantity’ column.