I'd like to implement a natural sorting using this:
public static IEnumerable<T> OrderByAlphaNumeric<T>(this IEnumerable<T> source, Func<T, string> selector)
{
int max = source
.SelectMany(i => Regex.Matches(selector(i), @"\d+").Cast<Match>().Select(m => (int?)m.Value.Length))
.Max() ?? 0;
return source.OrderBy(i => Regex.Replace(selector(i), @"\d+", m => m.Value.PadLeft(max, '0')));
}
(taken from Natural Sort Order in C#)
I have a dataview dv
which contains (among others) a column code_name
. I'd like to copy the data of this dataview into a new datatable dtNew
with a natural sort on the column code_name
. I guess the code should be something like:
DataTable dtNew = dv.Table.AsEnumerable().OrderBy(x => x.Field<string>("code_name"),OrderByAlphaNumeric<T>).CopyToDataTable();
But I don't understand anything in how to manipulate IEnumerable<T> OrderByAlphaNumeric<T>
in my context.