Reputation: 81
I wrote some code and got wrong sort.
Can anyone can explain?
List<double> ii = new List<double>() { 1.2043801874, 1.2077423953, 1.9349906628 ,2.3225299029 ,2.4545523619 ,
2.7680609859 , 2.8905747132 ,2.9940913416 , 3.0737675373 ,3.2299437327 , 3.260846366 ,3.3004602464 ,
3.4006307085 , 3.513391731 ,3.6214660094 ,4.8013057532 , 5.8670500438 ,6.1397306939 , 7.1040729766 ,
8.0858126519 , 1.2356543405 ,1.3799212012 ,1.3976222125 ,1.5461464187 ,1.7162840558 ,1.7511717786 ,
10.2544634898, 2.0069778978 ,2.0262575594 ,2.0868135866 ,2.1595079322 ,2.25748278 ,2.4881372712 ,
2.9191324991 ,3.0847910479 , 3.1859703596 ,5.39216447 ,5.3949224157 ,9.3404802167 ,};
DataTable input = new DataTable();
input.Columns.Add("i");
foreach(Double i in ii)
{
DataRow row = input.NewRow();
row["i"] = i;
input.Rows.Add(row);
}
DataTable Notsorted = input.AsEnumerable().OrderBy(x => x["i"]).CopyToDataTable();
the result picture
Upvotes: 0
Views: 320
Reputation: 216293
When you create a column you should specify its DataType otherwise it is assumed to be a string and the string "10.2544634898" is alphabetically lower than all strings that begins with "2", "3" etc...
input.Columns.Add("i", typeof(double));
Now, the sorting on the column will work treating correctly the numbers as numbers.
Upvotes: 1