Order Table by Text

Hi team,
I hope everyone is doing well.
I have a table as shown below:

image

My requirement is to order the table on the basis of ID and Relationship in such a way that SELF always shows up first, followed by SPOUSE, but SPOUSE may or may not always be present.

image

Is there a way to order Relationship by Text, instead of using its length? SON has three characters, so it will show up on first if I order by length, also there is the issue of SPOUSE not showing up in few instances.

Help would be most appreciated, thanks in advance!

Kind Regards,
Ashwin A.K

Hi,

Can you try the following?

First, set sorted relationship items as the following image, to string array ( let’s say keywords.)
Then use the following expression.

dt = dt.AsEnumerable.OrderBy(Function(r) Int32.Parse(r("ID").ToString)).ThenByDescending(Function(r) Array.IndexOf(keywords,r("Relationship").ToString)).CopyToDataTable

img20211103-2

If you want to also sort except SELF and SPOUSE, please add items (such as SON) at the beginning of the array.

Regards,

1 Like

I wasn’t aware of Array.Index, thank you for share your hard earned knowledge with the rest of us @Yoichi-san! :slightly_smiling_face:

Kind Regards,
Ashwin A.K

1 Like

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