How to custom sort datatable

I have below value in my input argument of type string
“Z12, Q34, D435, K0990, Z22, Q0289, D776, K1121, D879, Q7332”

I want to get them into datatable in following order. Z values first, then with D, followed by K and then Q values. Can anyone suggest how can i do it?

Lets assume you dont have null values

Assign Activity
arrCustomSort | String Array =
new String(){“Z”,“D”,“K”,“Q”}.Reverse().toArray

Assign Activity
dtOrdered =

(From d in yourDataTableVar.AsEnumerable
Let s = d("YourColName").toString.Trim.First().toString()
Let i = Array.IndexOf(arrCustomSort,s)
Order by i Descending
Select r = d).CopyToDataTable

maybe we also we use instead
Let s = d(“YourColName”).toString.Trim.FirstOrDefault().toString()

and can handle empty strings

What is yourDatatableVar? I am getting below error. Can you please let me know what i am doing wrong?

is the variable of the datatable which you want to sort
Adapt it with your variable’ s variable name

dtResult variable is of datatype: DataTable and is assigned with the ordered datatable

To prevent some mismatching in the understanding also have a look here


Ordered Ascending

Hi @santo.reddy2023

  • Assign: inputString → “Z12, Q34, D435, K0990, Z22, Q0289, D776, K1121, D879, Q7332”

  • Assign: stringArray → inputString.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)

  • sortedArray = stringArray.OrderBy(Function(s) If(s.StartsWith(“Z”), "1"c, If(s.StartsWith(“D”), "2"c, If(s.StartsWith(“K”), "3"c, "4"c)))).ThenBy(Function(s) s).ToArray()

  • Build DataTable: dtValues with one string column

  • For Each item in sortedArray (TypeArgument: String)

    • Add Data Row: {item} to dtValues