Specifying Sort Warning While Sorting Data
Possible Usage Scenarios
Please consider this textual data i.e. {11, 111, 22}. This textual data is sorted because, in terms of text, 111 comes before 22. But, if you want to sort this data not as text but as numbers, then it will become {11, 22, 111} because numerically 111 comes after 22. Aspose.Cells provides DataSorter.SortAsNumber property to deal with this issue. Please set this property true and your textual data will be sorted as numerical data. The following screenshot shows the sort warning shown by Microsoft Excel when textual data which looks like numerical data is sorted.
Sample Code
The following sample code illustrates the usage of DataSorter.SortAsNumber property as explained earlier. Please check its sample Excel file and output Excel file for more help.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
//Create workbook. | |
Workbook workbook = new Workbook(dataDir + "sampleSortAsNumber.xlsx"); | |
//Access first worksheet. | |
Worksheet worksheet = workbook.Worksheets[0]; | |
//Create your cell area. | |
CellArea ca = CellArea.CreateCellArea("A1", "A20"); | |
//Create your sorter. | |
DataSorter sorter = workbook.DataSorter; | |
//Find the index, since we want to sort by column A, so we should know the index for sorter. | |
int idx = CellsHelper.ColumnNameToIndex("A"); | |
//Add key in sorter, it will sort in Ascending order. | |
sorter.AddKey(idx, SortOrder.Ascending); | |
sorter.SortAsNumber = true; | |
//Perform sort. | |
sorter.Sort(worksheet.Cells, ca); | |
//Save the output workbook. | |
workbook.Save(dataDir + "outputSortAsNumber.xlsx"); |