-4

i got an output from a list into an excel file. To simplefile things it could look like this:

4 1 2 3

Now all i wanna do is, put it in the right order:

1 2 3 4

which i did with this code ( isSort contains 4 1 2 3 ):

        ...
        var isSortFin = new List<Item>();
        var FirstElement = isSort.First();
        foreach (var Itemd in toSort)
        {
            if (Itemd.Summary != FirstElement.Summary)
            {
                isSortFin.Add(Itemd);
            }
        }

        isSortFin.Add(FirstElement);
        return isSortFin;

now my output is:

3 2 1 4

and not the aspired:

1 2 3 4

where do i go wrong?

Timo N.
  • 341
  • 3
  • 12

1 Answers1

4

You could use OrderBy linq extension and sort element.

isSort=  isSort.OrderBy(x=>x).ToList(); 

If Item is an object, specify property name in OrderBy expression to sort on that property.

//ex.. 
isSort=  isSort.OrderBy(x=>x.Value).ToList();
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • 1
    If that's your answer, this question is a duplicate. Vote to close instead of answering. – CodeCaster Jun 10 '16 at 09:36
  • 1
    May be you are right, but that shouldn't be the reason for down vote. – Hari Prasad Jun 10 '16 at 09:37
  • I agreed to your second point, probably I could improve the answer including that point, but not agree with point size of the body is small. Anyways thanks for suggestions. – Hari Prasad Jun 10 '16 at 09:42
  • damn... why did i put roughly ~5 hours in a code ( which i didnt post here, i basically progammed my own sort method xD ) that can be done with a one liner... – Timo N. Jun 10 '16 at 09:54