0

I tried C# sorting order that has number and character.

List<EmployeesSTPList> employeeList = new List<EmployeesSTPList>();

employeeList.Add(new EmployeesSTPList { ID = "2", Name = "Employee 2" });
employeeList.Add(new EmployeesSTPList { ID = "1", Name = "Employee 1" });
employeeList.Add(new EmployeesSTPList { ID = "3", Name = "Employee 3" });
employeeList.Add(new EmployeesSTPList { ID = "10", Name = "Employee 10" });
employeeList.Add(new EmployeesSTPList { ID = "EMP002", Name = "Employee 02" });
employeeList.Add(new EmployeesSTPList { ID = "EMP003", Name = "Employee 03" });
employeeList.Add(new EmployeesSTPList { ID = "11", Name = "Employee 11" });
employeeList.Add(new EmployeesSTPList { ID = "4", Name = "Employee 4" });
employeeList.Add(new EmployeesSTPList { ID = "EMP010", Name = "Employee 010" });
employeeList.Add(new EmployeesSTPList { ID = "EMP001", Name = "Employee 01" });
employeeList.Add(new EmployeesSTPList { ID = "EMP011", Name = "Employee 011" });

employeeList = employeeList.OrderBy(a => a.ID, StringComparer.Ordinal).ToList();

foreach (var item in employeeList)
{
    <div> @item.ID</div>
}

Output is:

1
10
11
2
3
4
EMP001
EMP002
EMP003
EMP010
EMP011

But expected as,

1
2
3
4
10
11
EMP001
EMP002
EMP003
EMP010
EMP011

I have already tried the same with string[] using StringComparer.Ordinal which is working fine. But my model data contains in list.

Han
  • 3,052
  • 2
  • 22
  • 31
Sage
  • 127
  • 10
  • To get a custom sorting, you should write a custom comparer: https://stackoverflow.com/a/8975825/444165 – Arthur S. Jul 08 '19 at 07:34
  • You have to search for Natual Sorting scheme. It means you have to created your own comparer. – dotnetstep Jul 08 '19 at 08:05
  • Don't use the answer from the duplicate question, use the Windows API `StrCmpLogicalW()` [for example, like this](https://stackoverflow.com/a/23865465/106159). (Note: There are other similar answers using `StrCmpLogicalW()` on StackOverflow.) – Matthew Watson Jul 08 '19 at 08:43

1 Answers1

0

StringComparer.Ordinal does not check if the string is a number before it sorts. Instead, it does the following:

performs a simple byte comparison that is independent of language. This is most appropriate when comparing strings that are generated programmatically or when comparing case-sensitive resources such as passwords.

You would need a custom comparer that checks if the string is a number first. Possibly using a regex.

David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • 1
    I think this should be a comment. – SᴇM Jul 08 '19 at 07:36
  • I don't agree, he questions why StringComparer.Ordinal doesn't work, I answer that. He asks how it can be done, I answer that. Just because I don't provide a full working code sample does not mean that the question is not answered. – David Pilkington Jul 08 '19 at 07:50
  • OP didn't ask _"why `StringComparer.Ordinal` doesn't work"_, about `StringComparer.Ordinal` OP said _"I have already tried the same with `string[]` using `StringComparer.Ordinal` which is working fine."_. Also your answer about how can it be done IMHO is very abstract and doesn't provide the necessary answer to the question (I'm not saying it's wrong or something). – SᴇM Jul 08 '19 at 08:30
  • @SeM The OP must be mistaken about StringComparer.Ordinal, because it works the same regardless of whether it's used to sort a List or an array. In both cases, it will *not* work how he expects. – Matthew Watson Jul 08 '19 at 08:47