Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting a generic list of doubles

I have a generic list of doubles that show on the page like this:

1199.17
1199.17
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1161.67
1199.17
1349.17
1349.17
1349.17
1349.17
1349.17
1349.17
1311.67
1311.67
1311.67
1311.67
1311.67
1349.17
2174.17
2174.17
2174.17
2174.17
2136.67
2136.67
2136.67
2136.67
2174.17
2361.67
2361.67
2361.67
2361.67
2361.67
2361.67
2361.67
2361.67
2399.17
2849.17
2849.17
2849.17
2849.17
2849.17
2849.17
2849.17
2849.17
3111.67
3111.67
3111.67
3149.17

I am trying to order them so that the lowest double is first.

I tried doublePriceList.Sort() but this did not work.

How can I do this?

like image 322
phili Avatar asked Mar 08 '11 13:03

phili


2 Answers

using System.Linq;

and

var sortedList = doublePriceList.OrderBy(d => d);
like image 88
Bala R Avatar answered Sep 26 '22 02:09

Bala R


Sort (as per docs) works perfectly, although it isn't returning anything (isn't chainable):

var ds = new List<double>{
    2399.17,
    1199.17,
    // ...
};

ds.Sort();

foreach (double d in ds)
    Console.WriteLine(d);
like image 29
Simeon Avatar answered Sep 27 '22 02:09

Simeon