Sort DataTable with Multiple Columns(in a For Each)

Hi,

I have a config file for sorting columns. Data looks like;

ColumnName | SortingRule

Column1 ASC
Column2 DESC
Column3 ASC

I have to read config, then use a for each row, and sort columns. When i do this like below;

image

It sorts one by one. We don’t want that. How can i achieve this?

Thanks.

@balupad14 Have any thougths?

@ercan.nebiler Not sure what is the Output that you expect :sweat_smile: , based on that we can provide a solution.

@ercan.nebiler

Without using for each row, you can use as below

dt_Sort = From x In dt_Sort.AsEnumerable() Order By convert.Tostring(x(“Column1")),convert.ToString(x(“Column2”)) Select x).CopyToDataTable

Using Invoke Code:
dt_Sort.DefaultView.Sort = (“Column1_name ASC, Column2_name ASC”)
dt_Sort = datatableVar.DefaultView.ToTable

Hope this works

Thanks

4 Likes

Hi thanks for your answer.

Like i said i have a config file. Dt_Config
And a target file: Dt_Target

I will sort multiple columns. Which is dynamic. When i do this like image above, it sorts one by one. So it sorts first column. Then second. But first sort is becoming invalid. Because we sorted the other column. Clear now? :smiley:

Hi can’t hardcode this. It’s have to be dynamic. There can be 3 columns, next week can be 5 columns.

Hi @ercan.nebiler,

Take a look this thread with example

Thank you
Balamurugan.S

Thanks for your answer.

How can i make “create table” activity’s range dynamic?

Hi @balupad14,

I almost made the sorting with your help. But there is a problem.

image

I sorted this column DESC, But it’s not seeing these as numbers i guess. What do you suggest about it?

Thanks

What is the format ?

General, i am creating this excel with excel application scope.

You can use the previously mentioned approach using Invoke Code in a dynamic way as the Sort property on DataView takes a string.

“Column1_name ASC, Column2_name ASC”

You can read the config file and concatenate each sorting rule into a single string, then set it on the Sort property.

working beautifully! thanks

2 Likes