Uipath - Sort Datatable


I’m trying to sort a datatable variable which come from a .csv file.

I would like to sort the column name: “Column2”,

¿How it could be posible just using the uipath posibilities? ( Not writing in an Excel… )

I´ve been trying with the table.defaultview or table.select properties but i’m not able to sort it.

How it could be done?

Cheers, David

Hi David, here is how it works with the defaultview sortDataTable.xaml (9.3 KB)

You can do it with datatable select as well, like in datatable.select(expression, sortOrder). See more explanations on this approach here



Good Job

its very easy


I want to sort my data table with ‘n’ number of columns. For this I have to pass the variable in my expression.I want to use below expression but it is limited to two columns only.
datatablevariable = From x In datatablevariable.AsEnumerable() Order By convert.Tostring(x(“Column1")),convert.ToString(x(“Column2”)) Select x).CopyToDataTable

How can I achieve this?


So you want to sort by one column only? Or you want to sort by column1, then by column2, and so on?

If it is the first, then no need for a complicated linq statement. You can just specify the single column you’d like to sort in the datatable instead of creating a new Select statement. Here is a stackoverflow question asking how to do that, along with multiple answers that would work fine: .net - Sorting a Data Table - Stack Overflow

If it is the second, then you should use the .Orderby method for the first sort, then each sort thereafter you specify (in order) with the .ThenBy method

Hi Dave,

I want to sort my data table with multiple columns. Also, column’s names by which I want to sort data table are given as a input by the user so they can be either 2 or 3 or more than that.

Hi Dave,

My issue is resolved now. Thanks for providing that stack overflow link.
I just used the Invoke code activity and put below two lines:
datatableVar.DefaultView.Sort = (“Column1_name ASC, Column2_name ASC”)
datatableVar = datatableVar.DefaultView.ToTable
Now, I can put any number of columns here.