-1

I have a list containing data with string and number. I have to order it in ascending and descending order.

        id    data
    ------------------
        1     data1@%
        2     data10
        3     data
        4     @$data
        5     data2

I fetch the record and store it in list "List". Then I order it in ascending, but "data10" is coming in b/w "data1" and "data2". Below is my code

var o/p = List.OrderBy(x => x.data);

expected output — standard ascending order format.

anand
  • 1,559
  • 5
  • 21
  • 45
  • 1
    You need Alphanumeric sorting – Mrinal Kamboj Nov 17 '16 at 06:36
  • 3
    Possible duplicate of [Alphanumeric sorting using LINQ](http://stackoverflow.com/questions/5093842/alphanumeric-sorting-using-linq) – Martheen Nov 17 '16 at 06:36
  • There is no "standard" ascending order format. You can use the [comparisonType](https://msdn.microsoft.com/en-us/library/system.stringcomparison(v=vs.110).aspx) parameter of the [String.Compare](https://msdn.microsoft.com/en-us/library/e6883c06(v=vs.110).aspx) method to specify a specific comparison. – Manfred Radlwimmer Nov 17 '16 at 06:38
  • here the column contains alphanumeric, special characters, alpha – anand Nov 17 '16 at 06:43
  • @anand Doesn't matter, both the linked question's answers and Mrinal's answer will still work, because all it takes is treating the numeric part specially – Martheen Nov 17 '16 at 06:45

1 Answers1

2

How bout this, Copied from this Where list is your List variable.

var result = list.OrderBy(x => Regex.Replace(x, "[0-9]+", match => match.Value.PadLeft(10, '0')));

enter image description here

Community
  • 1
  • 1
Niar
  • 532
  • 2
  • 11
  • 23
  • Good one, I assume it works for all the Alpha numeric scenarios +1, however as you are copying from another answer, please try to add your own explanation – Mrinal Kamboj Nov 17 '16 at 07:03