This post was most recently updated on August 11th, 2019
Problem Statement: Sort the Alphanumeric values
Solution :
below is the Code that helped to sort the alphanumeric values
Example :
you have the list
var List = {a1,ba3,aa2,a2,ba2,aa3};
we can not sort this directly using
1 |
@foreach (var v in Model.List.OrderBy(x=>((x.values))).ToList()) { } |
so to sort this we need to PadNumbers to value and then sort the List.
with the help of below code
1 |
Regex.Replace(input, "[0-9]+", match => match.Value.PadLeft(10, '0')) |
we can pad the number to the string
Code
Model:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Mymodel { Public List<AlpahnumericList> List { get; set; } Public string PadNumbers(string input) { return Regex.Replace(input, "[0-9]+", match => match.Value.PadLeft(10, '0')); } } public class AlpahnumericList { Public string values { get; set; }; } |
View:
1 2 3 4 5 |
@foreach (var v in Model.List.OrderBy(x=>(Model.PadNumbers(x.values))).ToList()) { // show sorted List in view <div> v.values</div> } |