www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - why the sort result is different with C#

reply mzfhhhh <mzfhhhh foxmail.com> writes:
D code:
auto arr = ["b1=1", "b=2","a1=1", "a=2"];
writeln(arr.sort());

output:["a1=1", "a=2", "b1=1", "b=2"]

C# code:
var arr = new string[]{ "b1=1", "b=2", "a1=1", "a=2" };
Array.Sort(arr);

output:["a=2","a1=1","b=2","b1=1"]
Jan 21
next sibling parent Luis <luis.panadero gmail.com> writes:
On Friday, 22 January 2016 at 06:53:29 UTC, mzfhhhh wrote:
 D code:
 auto arr = ["b1=1", "b=2","a1=1", "a=2"];
 writeln(arr.sort());

 output:["a1=1", "a=2", "b1=1", "b=2"]

 C# code:
 var arr = new string[]{ "b1=1", "b=2", "a1=1", "a=2" };
 Array.Sort(arr);

 output:["a=2","a1=1","b=2","b1=1"]
I don't what string sorting criteria is using C#, but '9' < '=', so should be ["a1=1", "a=2", "b1=1", "b=2"]
Jan 22
prev sibling next sibling parent arturg <var.spool.mail700 gmail.com> writes:
On Friday, 22 January 2016 at 06:53:29 UTC, mzfhhhh wrote:
 D code:
 auto arr = ["b1=1", "b=2","a1=1", "a=2"];
 writeln(arr.sort());

 output:["a1=1", "a=2", "b1=1", "b=2"]

 C# code:
 var arr = new string[]{ "b1=1", "b=2", "a1=1", "a=2" };
 Array.Sort(arr);

 output:["a=2","a1=1","b=2","b1=1"]
auto arr1 = ["b1=1", "b=2", "a1=1", "a=2"]; auto arr2 = ["b1 = 1", "b = 2", "a1 = 1", "a = 2"]; arr1.sort.writeln; // ["a1=1", "a=2", "b1=1", "b=2"] arr2.sort.writeln; // ["a = 2", "a1 = 1", "b = 2", "b1 = 1"]
Jan 22
prev sibling parent mzfhhhh <mzfhhhh foxmail.com> writes:
i know the reason,C# have several compares methods.
String.Compare , String.CompareOrdinal

//now the output is same as the D code
Array.Sort(arr, string.CompareOrdinal);
Jan 22