Sort DataTable rows by length in c#

.net, c#, datatable

Solution

You could add an extra column to your `DataTable`, supplying an expression containing a call to the `len()` function, causing the column's values to be automatically computed:

table.Columns.Add("LengthOfName", typeof(int), "len(Name)");

Then you can simply sort on your new column before binding the `DataTable` to a grid to whatever kind of UI control you plan to use:

table.DefaultView.Sort = "LengthOfName";

Problem

How to do following scenario: I have some DataTable which contains for example some rows: 1.rowa 2.rowab 3.row 4.rowaba ... n. rowabba How to sort rows by lenght, not by name. I wan to to sort Table by length of fields.

Original source