Sorting all columns data in Excel

Hi Sir/ Madam

I have 2 excel files and I need to compare the data between these 2 files.
The columns will be same but data will be in different order.
Before compare the files, I need to sort by all columns and compare.
DataSorter object allows to sort only 3 columns, Is there any way to sort all columns?
Please advise.

@Svas78,

You can sort multiple columns and specify more than 3 keys for your requirements. See the following sample code for your reference.
e.g.
Sample code:

Workbook workbook = new Workbook("g:\\test2\\Bk_SortCols1.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;

DataSorter sorterCols = workbook.DataSorter;
sorterCols.AddKey(0, SortOrder.Ascending);//first column
sorterCols.AddKey(1, SortOrder.Ascending);//second column
sorterCols.AddKey(2, SortOrder.Descending);//third column
sorterCols.AddKey(3, SortOrder.Descending);//fourth column
sorterCols.AddKey(4, SortOrder.Ascending);//fifth column

CellArea cellArea2 = CellArea.CreateCellArea("A2","N8");
sorterCols.Sort(cells, cellArea2);

workbook.Save("g:\\test2\\out1.xlsx");

Please refer to above code segment and write/update your code accordingly. If you still could not implement it or find any issue, kindly do provide your sample (runnable) code and sample files to reproduce the issue, we will check it soon.

PS. please zip the files prior attaching here.

Hi Amjad Sah

This code is not working, it is sorting only 1st column. but I need to sort all columns from 1 to 5.

@Svas78
Testing by creating sample file, we are able to sort all columns from 1 to 5. Please check the attachment (16.9 KB).

The sample code as follows:

Workbook workbook = new Workbook(filePath + "sample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;

DataSorter sorterCols = workbook.DataSorter;
sorterCols.AddKey(0, SortOrder.Ascending);//first column
sorterCols.AddKey(1, SortOrder.Ascending);//second column
sorterCols.AddKey(2, SortOrder.Descending);//third column            
sorterCols.AddKey(3, SortOrder.Descending);//fourth column
sorterCols.AddKey(4, SortOrder.Ascending);//fifth column

CellArea cellArea = CellArea.CreateCellArea("A2", "E33");
sorterCols.Sort(cells, cellArea);

workbook.Save(filePath + "out_net.xlsx");

If you still have any questions or confusion, please provide your sample file and we will check it soon.

Dear John

It is working fabulously.
Thank yo so much for your help!

@Svas78
Thank you for your feedback. You are welcome. If you have any questions, please feel free to contact us.