How to Sort a Column or Multiple Columns (in different Orders) in a DataTable

This thread will help you to sort a column or Multiple Columns of Data Table using some simple techniques.

Suppose if we have a Data Table having 2 columns as shown below.
image

1. To Sort data in Ascending Order based on Column “KeyWord” using Linq Query :

Sort a column in Ascending Order using LinQ

Expression :

InputDt = (InputDt.AsEnumerable.OrderBy(Function(x) x(“KeyWord”).ToString)).CopyToDataTable

Result :
image

2. To Sort data in Descending Order based on Column “KeyWord” using Linq Query :

Sort a column in Descending Order using LinQ

Expression :

InputDt = (InputDt.AsEnumerable.OrderByDescending(Function(x) x(“KeyWord”).ToString)).CopyToDataTable

Result :
image

3. To Sort Data in Ascending order based on column “keyWord” using Sort Data Table Activity

Sort a Column using Sort Data Table Activity

Activity & It’s Property settings :
image
image

(In Properties panel Pass Input Datatable, Output Datatable, Column Name, Order)

4. To Sort the Data in ascending Order based on column “KeyWord” and in Descending Order based on column “EndDate”

Sort Multiple columns in different Order

Use Assign Activity and the expressions as below.

Assign InputDt.DefaultView.Sort = “[KeyWord] ASC, [EndDate] DESC”
Assign InputDt = InputDt.DefaultView.ToTable

Output :
image

Happy Automation!!

Thanks & Regards,
Rohith

3 Likes