using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Select a range
var range = workSheet["A1:D20"];
// Select a column(B)
var column = workSheet.GetColumn(1);
// Sort the range in ascending order (A to Z)
range.SortAscending();
// Sort the range by column(C) in ascending order
range.SortByColumn("C", SortOrder.Ascending);
// Sort the column(B) in descending order (Z to A)
column.SortDescending();
workBook.SaveAs("sortExcelRange.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Select a range
Private range = workSheet("A1:D20")
' Select a column(B)
Private column = workSheet.GetColumn(1)
' Sort the range in ascending order (A to Z)
range.SortAscending()
' Sort the range by column(C) in ascending order
range.SortByColumn("C", SortOrder.Ascending)
' Sort the column(B) in descending order (Z to A)
column.SortDescending()
workBook.SaveAs("sortExcelRange.xlsx")
Install-Package IronXL.Excel
Sort Excel Ranges in C#
Arranging data in alphabetical or value order makes human analysis of data feasible in Microsoft Excel. IronXL makes sorting Excel Columns, Rows, and Ranges extremely simple in C# and VB.NET.
Use the SortAscending or SortDescending methods on the selected range or column to apply sorting in the appropriate order. The SortByColumn method, however, requires two parameters: The first input is the column intended to be sorted and the second input is the sorting order.