tags:

views:

214

answers:

3

Using C# what is the best way to sort a List numerically? my list has items 5,7,3 and I would like them sorted 3,5,7. I know some longer ways, but I would imagine linq has a quicker way?

sorry this was end of day, my mind is else where it worked, didn't see it change the first time:(

+19  A: 

There's no need for LINQ here, just call Sort:

list.Sort();

Example code:

List<int> list = new List<int> { 5, 7, 3 };
list.Sort();
foreach (int x in list)
{
    Console.WriteLine(x);
}

Result:

3
5
7
Mark Byers
i tried that, it didnt seem to work... ill run it through the debug again
Spooks
sorry end of day, I was viewing it before the sort took place :( home time i suppose
Spooks
+2  A: 
var values = new int[] {5,7,3};
var sortedValues = values.OrderBy(v => v).ToList();   // result 3,5,7
Will
A: 

If you are looking or a sorting algorithm, you need something like this:

int[] input = { 5, 3, 7 };

// exchange sort 
for (int i = 0; i < (input.Length - 1); i++)
{
    for (int j = (i + 1); j < input.Length; j++) 
    {
        if (input[i] > input[j])
        {
            int temp = input[i];
            input[i] = input[j];
            input[j] = temp;
        }
    }
}

Its worth noting here that this is going to be n^2 time, it may be more efficent to simply use the .Sort() method, or even the Linq .OrderBy() method -- however, what I have here should work in any language / platform.

Nate Bross
PLEASE tell me you're not advocating use of a SelectionSort on a construct that can contain up to 2 billion items. I don't care how slow the List.Sort() implementation is, it's not THAT slow.
KeithS
@KeithS -- The example problem contains 3 items, it hardly matters that it is not the most efficent I even stated that in the answer. I'm not advocating anything. I specifically noted that it takes n^2 time to complete and that both `.Sort()` and `.OrderBy()` are likely faster and more efficent. What I posted is quite simply a language agnostic solution intended for a modest dataset. By the way, where did you come up with 2 billion items? Any alogrithm will be slow on 2 billion items.
Nate Bross
In .NET, a List<> can store as many items as it can index, which is int.MaxValue() (2,147,483,647). As for the example, most SO examples are simple to make stating the problem take less than a screen; that doesn't mean what he's actually doing is this simple. And as for your answer, because you suggested an N^2 sort, it is inferred that you think this is the best, or even an acceptable answer, and that the OP should use it. I disagree vehemently.
KeithS
@KeithS - I'm aware of the restrictions on a `List<T>` and the implications. If you read my answer, you'll see that I explicitly point out that it is in fact *not* the best; its simply an alternative solution. It might be of use to someone who finds this page from searching who is using something other than C#. I also think its safe to assume the OP is not using 2 billion items in the his/her List.
Nate Bross