IronXL How-Tos Sort Cell Range How to Sort Cell Range Chaknith Bin Updated:July 28, 2025 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. Get started with IronXL Start using IronXL in your project today with a free trial. First Step: Start for Free 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 Sort Columns Independently Example Use the SortAscending or SortDescending method on the selected range or column to apply sorting in the desired order. When applying sorting to a range with multiple columns, the SortAscending or SortDescending method will independently iterate and apply the sorting to 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 dataset. :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") $vbLabelText $csharpLabel Sort By Column Example The SortByColumn method allows you 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 to which you want to apply the sorting. :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") $vbLabelText $csharpLabel It is not possible to sort by multiple columns yet, for example, sorting by column A and then by column B. Frequently Asked Questions How to sort a cell range in Excel using C#? To sort a cell range in Excel using C#, you can use IronXL. First, download the library from NuGet, load your Excel spreadsheet, and select the range or column you want to sort. Apply the SortAscending or SortDescending method based on your desired sorting order. Can I sort Excel columns independently using C#? Yes, with IronXL, you can sort Excel columns independently in C#. Use the SortAscending or SortDescending methods on the selected range or column, which will apply sorting independently to each column. How can I sort Excel rows in descending order programmatically? To sort Excel rows in descending order programmatically using IronXL, load your Excel file and use the SortDescending method on the selected range or column. This will arrange your data in descending order. Is it possible to remove empty cells after sorting an Excel range using C#? Yes, after sorting an Excel range with IronXL, you can remove empty cells by using the Trim method. This will clean up your dataset by eliminating empty cells. How do I sort a range based on a specific column in Excel using C#? To sort a range based on a specific column in Excel using C#, use the SortByColumn method provided by IronXL. You need to specify the column index and the range to apply the sorting. Can I sort by multiple columns simultaneously in Excel using a C# library? No, currently IronXL does not support sorting by multiple columns simultaneously. You can sort by one column at a time using the SortByColumn method. How can I save the sorted Excel data programmatically? After sorting your Excel data using IronXL, you can save the sorted data to a new Excel file using the SaveAs method, ensuring your changes are preserved. What happens to empty cells when sorting Excel data in C#? When sorting Excel data using IronXL in C#, empty cells are pushed to either the top or bottom of the range. You can then remove these empty cells using the Trim method. Chaknith Bin Chat with engineering team now Software Engineer Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience. Ready to Get Started? Free NuGet Download Total downloads: 1,558,208 View Licenses