Sorting datatable according to multiple columns

I want to sort the datatable in such a way that the resulting datatable will have data arranged in the order of first column and the same values in second column should be grouped together.

Initial datatable

GN JMBWN Name
1 BBBB Arun
1 BBBB Ashwin
1 CCCC Vishwa
2 DDDD John
2 BBBB Sam
2 DDDD Jain

The output should look like

GN JMBWN Name
1 CCCC Vishwa
1 BBBB Arun
1 BBBB Ashwin
2 BBBB Sam
2 DDDD John
2 DDDD Jain

I tried some query but not able to get this desired result.

The query that i tried is
my datatable name is dtAdultTable.

“dtAdultTable.AsEnumerable.OrderBy(Function(x) x(0)).GroupBy(Function(x) x(1)).SelectMany(Function(x) x).CopyToDataTable()”

We can use the Query Syntax

(From d in dtAdultTable.AsEnumerable
Let s1 = CInt(d("GN").toString.Trim)
Let s2 = d("JMBWN").toString.Trim
Order by s1,s2
Select r=d).CopyToDataTable
1 Like

Hi @arun.v7

Can you try below query

dtAdultTable = dtAdultTable.AsEnumerable().
    OrderBy(Function(row) row("GN")).
    ThenBy(Function(row) row("JMBWN")).
    CopyToDataTable()

Regards,

Hi @arun.v7

You can try this Linq
dtAdultTable.AsEnumerable().OrderBy(Function(row) Cint(row(“GN”))).ThenBy(Function(row) row(“JMBWN”).ToString).CopyToDataTable

iam getting the result like this,
1,BBBB,Arun
1,BBBB,Ashwin
1,CCCC,Vishwa
2,BBBB,Sam
2,DDDD,John
2,DDDD,Jain

But i want the same values in second column to be together, also the first column should be in order.

iam getting the result like this,
1,BBBB,Arun
1,BBBB,Ashwin
1,CCCC,Vishwa
2,BBBB,Sam
2,DDDD,John
2,DDDD,Jain

But i want the same values in second column to be together, also the first column should be in order. Here the issue iam facing is that always when i sort the (2,BBBB,Sam) is not coming together with the other BBBB values. But when i sort it and achieve this condition, the other condition that the first column should be in order will break.

iam fairly new to UiPath, please let me know how to use this code?

same as you used the Method syntax, Assign Activity

[HowTo] LINQ (VB.Net) Learning Catalogue - Help / Something Else - UiPath Community Forum

I tried the solution suggested by you, but iam not getting the second column values together when sorting the first column. Basically i want the same values in second column to come together while maintaining the order in the first column

Can you try this and let me know if it works
dtAdultTable.AsEnumerable().OrderBy(Function(row) Cint(row(“GN”))).ThenBy(Function(row) row(“JMBWN”).ToString).ThenBy(Function(row) row(“Name”).ToString).CopyToDataTable

Sorry i think you misunderstood when i said second column. The column “Name” is not relevent when it comes to my sorting condition. I meant first column as “GN”, and the second column as “JMBWN”.

@arun.v7
keep one thing in mind:

grouping on first column and sorting on first column will create the series
111,222 …

sorting on second column you should decide the criteria for the sorting Ascending, Descending

maybe the following is needed:

(From d in dtAdultTable.AsEnumerable
Let s1 = CInt(d("GN").toString.Trim)
Let s2 = d("JMBWN").toString.Trim
Order by s1,s2 DESCENDING
Select r=d).CopyToDataTable

But we cannot do:
The image shows a table with columns "GN," "JMBWN," and "Name," with handwritten notes indicating a comparison between descending (desc) and ascending (asc) order sorting. (Captioned by AI)

UPD1 - Visualizations:
The image displays two data tables, "dtData" and "dtData2," with columns for identifiers and names, showing different sets of rows. (Captioned by AI)

Use Assign activity.
dt1=(
From row In dt
Order By row(“GN”),row(“JMBWN”)
Select row
).CopyToDataTable

I just gave the value as CCCC, BBBB, since i cannot paste the actual data here. The only criteria for me is that the same values should be grouped together and also the first column must be in order. Is this possible?.

then we recommend to reformulate the requirement sharp and clear or to provide matching sample data

in general we address by

  • grouping data
  • sorting data

I don’t think there is any reliable algorithm to fulfill your requirement.

E.g. below input data can’t be sorted according your requirement
1,DDDD,Arun
1,BBBB,Ashwin
1,CCCC,Vishwa
2,BBBB,Sam
2,DDDD,John
2,DDDD,Jain

Cheers

1 Like

I will list down the specifics. The first column is a group number which will always be a numeric value ranging from 1 to 6. The second column can be any string but there can be only two unique values for each group according to the first column. Only one value can repeat in more than one group and that value is fixed. For example if we assume that BBBB is the value which repeats in each group(it can repeat doesnt mean it will repeat, it may happen that it might not be present in any group), then there will only be one other string in group one, but that string can repeat any number of time according to the group strength.

the scenario you told will not happen since there can only be two unique values for a single group of first column. i.e, in 1,1,1 there can only be two unique values in second column. same with 2,2,2

image

image

In your sample answer, you have Arun and Ashwin sorted wrongly!

Thank you so much for checking.
In your code i could see that there’s some sorting according to the column “Name”. But i dont want to sort the data based on the column “Name”. I want the rows having the same “JMBWN” value to be together and the first column to be in order.
I got the result from your code, but does this solution satisfy the above criteria even if values in “Name” column changes?