sorting a generic list of doubles
Asked Answered
U

3

5

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?

Uninterrupted answered 8/3, 2011 at 13:33 Comment(3)
It should work, you need to show some of your code how and where you sort the list and where you display it.Tajuanatak
When you say that something doesn't work, describe the way in which it doesn't work. Ideally, produce a short but complete program which demonstrates the problem.Hue
Note that it is advisable to use decimal for prices. This due to rounding differences.Djakarta
M
17
using System.Linq;

and

var sortedList = doublePriceList.OrderBy(d => d);
Multilingual answered 8/3, 2011 at 13:37 Comment(0)
V
10

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);
Verada answered 8/3, 2011 at 13:40 Comment(0)
V
2

Try this if you want to print them out

foreach( double d in doublePriceList.OrderBy( d => d ) )
{
  //print d
}
Venomous answered 8/3, 2011 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.