How to Sort Cell Range
Sorting data in alphabetical or value order is essential for data analysis in Microsoft Excel. With IronXL, sorting Excel columns, rows, and ranges becomes straightforward in C# and VB.NET.
How to Sort Cell Range in Excel
- Download C# library to sort cell range
- Load existing Excel spreadsheet or create new one
- Select range or column to be sorted
- Apply SortAscending or SortDescending method based on desired order
- Sort a range based on a particular column with SortByColumn method

Install with NuGet
Install-Package IronXL.Excel
Sort Cell Range Example
Use SortAscending
or SortDescending
method on the selected range or column to apply sorting in the desired order.
When applying sorting on a range with multiple columns, the SortAscending
or SortDescending
method will independently iterate and apply the sorting on each column.
These methods push any empty cells to either the top or bottom of the range. To remove these empty cells, you can use the Trim method after sorting. This will eliminate the empty cells, ensuring a clean and organized data set.
:path=/static-assets/excel/content-code-examples/how-to/sort-cells-range.cs
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Select a column(A)
var column = workSheet.GetColumn(0);
// Sort column(A) in ascending order (A to Z)
column.SortAscending();
// Sort column(A) 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 column(A)
Private column = workSheet.GetColumn(0)
' Sort column(A) in ascending order (A to Z)
column.SortAscending()
' Sort column(A) in descending order (Z to A)
column.SortDescending()
workBook.SaveAs("sortExcelRange.xlsx")

Sort By Column
SortByColumn
method allow to sort a range based on a specified column. This method requires two parameters: the first parameter is the column you want to sort by, and the second parameter is the range you want to apply the sorting to.
:path=/static-assets/excel/content-code-examples/how-to/sort-cells-sort-by-column.cs
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Select a range
var range = workSheet["A1:D10"];
// Sort the range by column(B) in ascending order
range.SortByColumn("B", SortOrder.Ascending);
workBook.SaveAs("sortRange.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Select a range
Private range = workSheet("A1:D10")
' Sort the range by column(B) in ascending order
range.SortByColumn("B", SortOrder.Ascending)
workBook.SaveAs("sortRange.xlsx")
