We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Understanding Sorting Limitations

Hi,


I have a situation, where I’ll need to perform multiple Sort orders, example: Say I have multiple number columns say (12) and I need to perform a sort based on a specific Order.
I’ve attached an image to help better explain the situation, now the challenge is I went through and reviewed your documentation and based on that I cannot apply more than 3 levels for sorting.
Please advise if there is any other way of achieving the same result as shown in the screen shot.

Hi,


Thanks for your posting and using Aspose.Cells.

Please use the AddKey() method of DataSorter to add more keys. Please see the following sample code, its source excel file and the output excel file. I have also attached the screenshot showing the order of values before and after sorting for illustration.

C#
Workbook wb = new Workbook(“sample.xlsx”);

Worksheet ws = wb.Worksheets[0];

CellArea ca = CellArea.CreateCellArea(“A1”, “E35”);

DataSorter sorter = wb.DataSorter;

sorter.AddKey(0, SortOrder.Ascending);
sorter.AddKey(1, SortOrder.Ascending);
sorter.AddKey(2, SortOrder.Ascending);
sorter.AddKey(3, SortOrder.Ascending);
sorter.AddKey(4, SortOrder.Ascending);

sorter.Sort(ws.Cells, ca);

wb.Save(“output.xlsx”);

Java
<span style=“color: rgb(255, 20, 147); font-family: “Courier New”; font-size: small;”>Workbook wb = new Workbook(dirPath + “sample.xlsx”);

Worksheet ws = wb.getWorksheets().get(0);

CellArea ca = CellArea.createCellArea(“A1”, “E35”);

DataSorter sorter = wb.getDataSorter();

sorter.addKey(0, SortOrder.ASCENDING);
sorter.addKey(1, SortOrder.ASCENDING);
sorter.addKey(2, SortOrder.ASCENDING);
sorter.addKey(3, SortOrder.ASCENDING);
sorter.addKey(4, SortOrder.ASCENDING);

sorter.sort(ws.getCells(), ca);

wb.save(dirPath + “output.xlsx”);