How to sort the datatable column if numbers are 123-1,123-2,123-3

Hello friends can anyone help me to sort the below values of datatable column


I want in decending order like MED123-9 should be first in the datatable after sorting.

Thanks in advance.


try this Linq Query

yourDataTable.AsEnumerable().OrderByDescending(Function(row) row.Field(Of String)("ID")).CopyToDataTable()

1 Like

Hi @aslam_ali1

=> Use the “Read Range” activity to read your DataTable into UiPath.

=> Use an Assign activity to sort the DataTable in descending order using LINQ. Here’s an example of the expression you can use:

yourDataTable = yourDataTable.AsEnumerable().OrderByDescending(Function(row) row("ColumnName")).CopyToDataTable()

Replace yourDataTable with the name of your DataTable variable and “ColumnName” with the name of the column you want to sort by.

Hope it helps

1 Like


dt1.AsEnumerable.OrderByDescending(function(x) x(0)).CopyToDataTable


1 Like

We can setup a very detailed sorting and also could make more strict

Assumption: We can rely on the Pattern: X+D±D+ | X=Letter, D=digit, +1 or more

Assign activity
dtSorted =

(From d in YourDataTableVar.AsEnumerable
Let rt = System.Text.RegularExpressions.Regex.Match(d("ID").toString.Trim, "^.+?(?=\-)").Value 
Group d by k=rt into grp = Group
From g in grp
Let spos = System.Text.RegularExpressions.Regex.Match(g("ID").toString.Trim, "(?<=\-)\d+").Value
Let ipos = CInt(spos) 
Order by ipos DESCENDING
Select r = g).CopyToDataTable

with this more granuarly filtering we approaching to overcome lexically order issues and can also more fine adapt

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.