Sort datatable data based on one column

Example: I have a csv file like this.

Name,Price,Brand
iphone1,2000,apple
galaxy1,2040,samsung
iphone2,3000,apple
mate1,3020,huawei

I want to order it by the brand so it looks like this:
Brand,Name,Price
apple iphone1,2000
iphone2,3000
samsung galaxy1,2040
huawei mate1,3020

what activity do i use and how?

1 Like

Hi @clumsily
did u need end result like this
image

Then check this sample flow
Main.xaml (9.9 KB)

Regards,
Nived N

Hi @clumsily,

You can use Sort Data Table Activity for this.

It is located under the Programming>DataTable categories from Activity Panel.

Step1: To perform the sorting process under your data table (not sorted) place Sort DataTable and indicate the Input DataTable you want to sort.
Eg: dt1
image

Step2: Next, create a Data Table variable that stores the Sorted DT.
Eg: dt2
image

Step3: Under the Sorting Column there are several ways to locate which column you want the sort to perform.

Try using the third one which is “Name” and Input “Name” (the column from your table).

image

For the Output I used Write Range and send the sorted table as the input.
image

RESULT:
image

Happy Automation,
Ken

Hi @clumsily

Another way to achieve the same.

dt.AsEnumerable.OrderBy(Function (r) r("Brand").ToString).CopyToDataTable.DefaultView.ToTable(False, {"Brand", "Name", "Price"})

Finally you can write dt in excel file using Write Range

3 Likes

Hi @clumsily,

I think you are looking for a Groupby not Sort.

  • Groupby on Brand column.

@Kenneth_Balana, @kumar.varun2 may be you can assist with Groupby as well.

1 Like

GroupBy: Display the name of the item of each Brand.

Let:
dtBrand as DataTable output from read range activity
item as “Brand” as the key and
row as the data row value from each brand.

First For Each:
Using For each activity with Argument Type IGrouping: key of string and element of data row.

Set the values property with:
dtBrand.AsEnumerable().GroupBy(Function (li) li.Field(Of String)("Brand"))
image

Then log the Brand Name using
item.key
image

Second For Each:
Using Log Message activity to list the row values of a Brand. With a message object of:

"Item Name: "+row.Field(Of String)("Name")
image

Workflow Overview:

Input:
image

Output:
image