C# Code :
public class Test
{
public static void Main()
{
List<string> list = new List<string> { "johnsmith@mail.com", "john_newyork@mail.com", "john00@mail.com" };
list.Sort();
for(int i=0;i<list.Count;i++){
Console.WriteLine(list[i]);
}
}
}
Result :
john_newyork@mail.com
john00@mail.com
johnsmith@mail.com
Java Code :
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
List<String> list = new ArrayList<String>();
list.add("johnsmith@mail.com");
list.add("john_newyork@mail.com");
list.add("john00@mail.com");
Collections.sort(list);
System.out.println(list);
}
}
Result :
[john00@mail.com, john_newyork@mail.com, johnsmith@mail.com]
Ascii value of '_' is 95 and ascii value of '0' is 48. So ideally '0' should come before '_' in string sorting. Why C# sort places '_' before '0'?
But when I tried comparing the characters '_' and '0'. Both the languages compare properly and keep '0' before '_'.
Console.WriteLine('0'>'_'); -> False (C#)
System.out.println('0'>'_'); -> false (Java)