Hello,
I have this table and I would like to sort by column “Sequence” but only for the sub part (the items). Let me show you the input and the output expected
dt_Input
Reference
Sequence
Package 01
900
item AA
12
item BC
11
item TT
16
Package 02
910
item MM
36
item CC
32
item XY
38
item FA
30
dt_Output
Reference
Sequence
Package 01
900
item BC
11
item AA
12
item TT
16
Package 02
910
item FA
30
item CC
32
item MM
36
item XY
38
I will always get the word “Package” for example for the main part. I don’t know if I need to split into several tables. I could have more rows with Package 03 and sub items, Package 4, etc.
Maybe I could do this with linq.
o achieve the sorting of the sub-items while keeping the packages in their original order, you can follow these steps in UiPath:
Read the input DataTable (dt_Input).
Iterate through the rows of the DataTable to identify the package rows and the item rows.
Create a new DataTable (dt_Output) with the same structure as dt_Input.
Use a temporary DataTable to hold the items for each package.
Sort the items within each package by the “Sequence” column.
Add the sorted items back to dt_Output under the corresponding package.
' Read the input DataTable
Dim dt_Input As DataTable = ... ' Your code to read the input DataTable
' Initialize the output DataTable with the same structure as dt_Input
Dim dt_Output As DataTable = dt_Input.Clone()
' Temporary DataTable to hold items for sorting
Dim tempItems As DataTable = dt_Input.Clone()
' Variable to keep track of whether we are within a package
Dim isPackage As Boolean = False
For Each row As DataRow In dt_Input.Rows
' Check if the current row is a package
If row("Reference").ToString.StartsWith("Package") Then
' If we were in a package, sort the items and add them to dt_Output
If isPackage AndAlso tempItems.Rows.Count > 0 Then
' Sort the temporary items DataTable
Dim sortedRows = tempItems.AsEnumerable().OrderBy(Function(r) r.Field(Of Integer)("Sequence")).CopyToDataTable()
For Each sortedRow As DataRow In sortedRows.Rows
dt_Output.ImportRow(sortedRow)
Next
' Clear the temporary items DataTable for the next package
tempItems.Clear()
End If
' Add the package row to dt_Output
dt_Output.ImportRow(row)
' Set the flag to true as we are now in a package
isPackage = True
Else
' Add the item row to the temporary DataTable
tempItems.ImportRow(row)
End If
Next
' After the last package, sort and add any remaining items
If isPackage AndAlso tempItems.Rows.Count > 0 Then
Dim sortedRows = tempItems.AsEnumerable().OrderBy(Function(r) r.Field(Of Integer)("Sequence")).CopyToDataTable()
For Each sortedRow As DataRow In sortedRows.Rows
dt_Output.ImportRow(sortedRow)
Next
End If
' Now dt_Output contains the sorted items under each package
Thank you, I understand the logic, but I get an error. I added some logs and it seems that something is wrong with the line
’ Add the package row to dt_Output
dt_Output.ImportRow(row)
I don’t understand why I cannot Import the row.
I tried to update from dt_Output.ImportRow(row) to dt_Output.Rows.Add(row) but I get the same exception when I run the code “RemoteException wrapping System.NullReferenceException: Object reference not set to an instance of an object”
Thank you all for your help. The solution from @lojyehuang works great. I added one line inspired by the code provided by @Shiva_Nikhil and I updated my arguments accordingly.