Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer



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;

// This code snippet demonstrates how to load an Excel workbook, access its default worksheet,
// select the first column, sort it, and save the changes to a new file.

// Load the workbook from the specified Excel file
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Get the default worksheet in the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Select a column (A) by its index (0-based index, i.e., first column)
var column = workSheet.GetColumn(0);

// Sort the selected column in ascending order (alphabetical order: A to Z)
column.SortAscending();

// Optionally, if you need to sort the column in descending order (Z to A), uncomment the line below
// column.SortDescending();

// Save the modified workbook to a new Excel file
workBook.SaveAs("sortExcelRange.xlsx");
Imports IronXL



' This code snippet demonstrates how to load an Excel workbook, access its default worksheet,

' select the first column, sort it, and save the changes to a new file.



' Load the workbook from the specified Excel file

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")



' Get the default worksheet in the workbook

Private workSheet As WorkSheet = workBook.DefaultWorkSheet



' Select a column (A) by its index (0-based index, i.e., first column)

Private column = workSheet.GetColumn(0)



' Sort the selected column in ascending order (alphabetical order: A to Z)

column.SortAscending()



' Optionally, if you need to sort the column in descending order (Z to A), uncomment the line below

' column.SortDescending();



' Save the modified workbook to a new Excel file

workBook.SaveAs("sortExcelRange.xlsx")
$vbLabelText   $csharpLabel
Sort Ascending and Descending

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
// Import the necessary library for Excel operations.
using IronXL;

// Load an existing Excel workbook from a specified file path.
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Access the default worksheet in the loaded workbook.
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Define a range of cells in the worksheet from A1 to D10.
var range = workSheet["A1:D10"];

// Sort the defined range of cells by the values in column B in ascending order.
// The SortOrder.Ascending enum value is used to specify the order.
range.SortByColumn("B", SortOrder.Ascending);

// Save the modified workbook to a new file.
workBook.SaveAs("sortRange.xlsx");

// Note: Ensure that the "IronXL" package is installed and properly referenced in your project.
// You can install it via NuGet package manager.
' Import the necessary library for Excel operations.

Imports IronXL



' Load an existing Excel workbook from a specified file path.

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")



' Access the default worksheet in the loaded workbook.

Private workSheet As WorkSheet = workBook.DefaultWorkSheet



' Define a range of cells in the worksheet from A1 to D10.

Private range = workSheet("A1:D10")



' Sort the defined range of cells by the values in column B in ascending order.

' The SortOrder.Ascending enum value is used to specify the order.

range.SortByColumn("B", SortOrder.Ascending)



' Save the modified workbook to a new file.

workBook.SaveAs("sortRange.xlsx")



' Note: Ensure that the "IronXL" package is installed and properly referenced in your project.

' You can install it via NuGet package manager.
$vbLabelText   $csharpLabel
Sort by Specific Column

It is not possible to sort by multiple columns yet, for example, sorting by column A and then by column B.

Frequently Asked Questions

What is IronXL?

IronXL is a C# library that allows developers to work with Excel files without the need for Microsoft Excel Interop.

How do I get started with sorting cells using IronXL?

To get started with sorting cells using IronXL, download the library from NuGet, load an existing spreadsheet or create a new one, and then use the sorting methods such as SortAscending or SortDescending on your selected range or column.

How can I sort a cell range in ascending order using IronXL?

To sort a cell range in ascending order using IronXL, load your Excel file, specify the range, and apply the SortAscending method. Finally, save the changes to a new Excel file.

What methods are available for sorting in IronXL?

IronXL provides methods such as SortAscending and SortDescending for sorting ranges or columns. Additionally, the SortByColumn method allows sorting based on a specific column.

Can I sort by multiple columns using IronXL?

No, currently it is not possible to sort by multiple columns simultaneously using IronXL.

What happens to empty cells when sorting with IronXL?

When sorting with IronXL, empty cells are pushed either to the top or the bottom of the range. You can use the Trim method to remove empty cells after sorting.

How do I sort a range based on a specific column using IronXL?

To sort a range based on a specific column using IronXL, use the SortByColumn method, specifying the column index and the range to apply the sorting.

Is it possible to save the sorted Excel file using IronXL?

Yes, after sorting your Excel data using IronXL, you can save the sorted file to a new Excel file using the SaveAs method.

Chaknith related to Sort By Column Example
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.