How to sort data within the same datatable with oldest to newest?

I have a column in datatable,need to sort the column oldest to newestaccording to the year,month and number.

Below is the table

Id
ISS-2023JUL-0023
ISS-2023JAN-0012
ISS-2023APR-2880
ISS-2022JAN-1234
ISS-2022APR-3425

And the output after sort should be like below.

Id
ISS-2022JAN-1234
ISS-2022APR-3425
ISS-2023JAN-0012
ISS-2023APR-2880
ISS-2023JUL-0023

Please help me to solve this.

@yashashwini2322
did you try sort datatable activity.

  1. Read the data from your Excel file and store it in a DataTable, let’s call it “inputDataTable”.
  2. Use the “Sort Data Table” activity to sort the “Id” column in ascending order. Configure the activity as follows:
  • Input: Set it to “inputDataTable”.
  • Output: Create a new DataTable variable, let’s call it “sortedDataTable”.
  • Sort Order: Select “Ascending”.
  • Column Sort:
    • Column Name: Set it to “Id”.
    • Sort Order: Select “Ascending”.
  1. The “sortedDataTable” will now contain the sorted data. You can use the DataTable in

Assign Acitivity
dtOrdered =

(From d in YourDataTableVar.AsEnumerable()
Let sd = d("Id").ToString.Trim
Let sp = System.Text.RegularExpressions.Regex.Match(sd, "(?<=\-).*?(?=\-)").Value
Order by CDate(sp).DateTime
Select r=d).CopyToDataTable

Extending above to also incude the number we can do:

dtOrdered =

(From d in YourDataTableVar.AsEnumerable()
Let sd = d("Id").ToString.Trim
Let sp = System.Text.RegularExpressions.Regex.Match(sd, "(?<=\-).*?(?=\-)").Value
Let np =  System.Text.RegularExpressions.Regex.Match(sd, "(?<=\-)\d+?$").Value
Order by CDate(sp).Date, CInt(np)
Select r=d).CopyToDataTable
1 Like

I tried but it’s giving the error …datetime is not a member of date

@yashashwini2322
Try this one
dt.AsEnumerable().OrderBy(Function(row) DateTime.ParseExact(row("Id").ToString().Substring(4, 7), "yyyyMMM", CultureInfo.InvariantCulture)) .ThenBy(Function(row) Integer.Parse(row("Id").ToString().Substring(11))) .CopyToDataTable()

Order by CDate(sp).Date, CInt(np)

refer to the updated LINQ

1 Like

Thank you .it worked

Perfect, so the topic can be closed by:
Forum FAQ - How to mark a post as a solution - News / Tutorials - UiPath Community Forum

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