Sort datatable for subpart rows

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.

Thank you for your help

Hi @Vandekamp

Try this

SortedRows = (From row In dt_Input.AsEnumerable()
                  Order By If(row.Field(Of String)("Reference").StartsWith("Package"), 0, 1), 
                           If(row.Field(Of String)("Reference").StartsWith("Package"), 
                              row.Field(Of Integer)("Sequence"), 0) Ascending
                  Select row).CopyToDataTable()

Regards,

Thank you, but the result is not as expected

o achieve the sorting of the sub-items while keeping the packages in their original order, you can follow these steps in UiPath:

  1. Read the input DataTable (dt_Input).
  2. Iterate through the rows of the DataTable to identify the package rows and the item rows.
  3. Create a new DataTable (dt_Output) with the same structure as dt_Input.
  4. Use a temporary DataTable to hold the items for each package.
  5. Sort the items within each package by the “Sequence” column.
  6. 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”

@Vandekamp

I think it is not a good way but still you can prefer

Sequence1.xaml (11.0 KB)

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.

1 Like

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