How to sort data from left to right

Hi,

I want to perform this operation using aspose.cells for .NET

a b c d
1 2 3 4
23 24 25 26
100 20 30 250 Before Sort

d a c b After Sort
4 1 3 2
26 23 25 24
250 100 30 20 Sort Key

The sort key is the last column, could you please help me out? I tried sorting using the following snippet and it did not work.

DataSorter sorter = ReportWorkbook.DataSorter;

// Descending

sorter.Order1 = sortOrder;

sorter.Order2 = sortOrder;

sorter.HasHeaders = false;

sorter.CaseSensitive = false;

sorter.Key1 = nReport1ContentStartColumn + 2;

sorter.Key2 = nReport1ContentStartColumn + 3;

sorter.SortLeftToRight = true;

CellArea sortCellArea = new CellArea();

sortCellArea.StartRow = nReport1ContentStartRow - 1;

sortCellArea.EndRow = nReport3ContentEndRow;

sortCellArea.StartColumn = nReport1ContentStartColumn + 2;

sortCellArea.EndColumn = nReport2ContentEndColumn;

sorter.Sort(ReportCells, sortCellArea);

a, b, c, d are column headers, am I missing something simple?

Regards,

Pradeep

Amjad, Could you help with this request?

Hi,

I am not sure about your sample code pointer/objects value, but I think it works fine as expected. I have used a template file (attached) with your desired data in it, I used the following sample code to sort the data (left to right), it works as MS Excel. The output file is attached too.

Sample code:
Workbook workbook = new Workbook(“e:\test\mysortingkey.xlsx”);

DataSorter dataSorter = workbook.DataSorter;
dataSorter.Key1 = 0;
dataSorter.Order1 = Aspose.Cells.SortOrder.Descending;
dataSorter.Key2 = 1;
dataSorter.Order2 = Aspose.Cells.SortOrder.Descending;
dataSorter.HasHeaders = false;
dataSorter.CaseSensitive = false;
dataSorter.SortLeftToRight = true;

CellArea area = new CellArea();
area.StartRow = 0;
area.EndRow = 3;
area.StartColumn = 0;
area.EndColumn = 3;
dataSorter.Sort(workbook.Worksheets[0].Cells, area);

workbook.Save(“e:\test\outmysortingkey1.xlsx”);


If you still could not evaluate, kindly do manually create your desired file with sorted data and post it here with your sample code (same as mine), we will check it soon.

Thank you.

Hi Amjad

My requirement - the sort should not be based on the first row, it should be based on last row

d c b a
4 3 2 1
26 25 24 23
250 30 20 100

the sort should be based on values in row4

row 4 should look like 250, 100, 30, 20

Regards,

Pradeep

Hi,

I think you may try for your need:

Workbook workbook = new Workbook(“e:\test\mysortingkey.xlsx”);

DataSorter dataSorter = workbook.DataSorter;
dataSorter.Key1 = 3;
dataSorter.Order1 = Aspose.Cells.SortOrder.Descending;
dataSorter.Key2 = 2;
dataSorter.Order2 = Aspose.Cells.SortOrder.Descending;
dataSorter.HasHeaders = false;
dataSorter.CaseSensitive = false;
dataSorter.SortLeftToRight = true;

CellArea area = new CellArea();
area.StartRow = 0;
area.EndRow = 3;
area.StartColumn = 0;
area.EndColumn = 3;
dataSorter.Sort(workbook.Worksheets[0].Cells, area);

workbook.Save(“e:\test\outmysortingkey1.xlsx”);