-6
public class Checkit
{
  public string ID{ get; set; }
  public string b{ get; set; }
  public string c{ get; set; }
}

I have a class and I use also List<T> like this:

Checkitcs = new Checkit();
List<Checkit> Lst_Ch = new List<Checkit>();

and I want to sort by ID. For example:

Lst_Ch[1] = { ID=123 , b='afasfa' , c='afagas'}
Lst_Ch[2] = { ID=124 , b='afgasf' , c='afagas'}
Lst_Ch[3] = { ID=523 , b='afasfa' , c='afagas'}
Lst_Ch[4] = { ID=123 , b='afasfa' , c='afagas'}
Lst_Ch[5] = { ID=523 , b='afasfa' , c='afagas'}
Lst_Ch[6] = { ID=105 , b='afasfa' , c='afagas'}

and I want to Sort like that:

Lst_Ch[6] = { ID=105 , b='afasfa' , c='afagas'}
Lst_Ch[1] = { ID=123 , b='afasfa' , c='afagas'}
Lst_Ch[4] = { ID=123 , b='afasfa' , c='afagas'}
Lst_Ch[2] = { ID=124 , b='afasfa' , c='afagas'}
Lst_Ch[5] = { ID=523 , b='afasfa' , c='afagas'}
Lst_Ch[5] = { ID=523 , b='afasfa' , c='afagas'}

How can I sort this List?

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
jeremmy p
  • 49
  • 4
  • 2
    The code you've got won't even compile, which makes it harder to help you. – Jon Skeet Nov 05 '13 at 18:21
  • I try to group by but i cannot achieve. Like this: 'Lst_Ch.GroupBy(x => x.ID)' – jeremmy p Nov 05 '13 at 18:23
  • 1
    [`Enumerable.OrderBy` Method](http://msdn.microsoft.com/en-us/library/bb534966.aspx) and `List` Indexes in this collection are zero-based. – Harrison Nov 05 '13 at 18:27
  • 1
    I'm assuming the b value for the first item with ID 124 is wrong? (Apart from the compilation problems you'll have) – Bart Nov 05 '13 at 18:30

2 Answers2

1

Your example code will not compile, because the ID property of your class needs to be an int instead of a string and you need to put double quotes around your string values, like this:

public class Checkit
{
    public int ID { get; set; }
    public string b { get; set; }
    public string c { get; set; }
}

List<Checkit> Lst_Ch = new List<Checkit>();
Lst_Ch.Add(new Checkit
{
    ID = 123,
    b = "afasfa",
    c = "afagas"
});
Lst_Ch.Add(new Checkit
{
    ID = 124,
    b = "afasfa",
    c = "afagas"
});
Lst_Ch.Add(new Checkit
{
    ID = 523,
    b = "afasfa",
    c = "afagas"
});
Lst_Ch.Add(new Checkit
{
    ID = 123,
    b = "afasfa",
    c = "afagas"
});        
Lst_Ch.Add(new Checkit
{
    ID = 523,
    b = "afasfa",
    c = "afagas"
});
Lst_Ch.Add(new Checkit
{
    ID = 105,
    b = "afasfa",
    c = "afagas"
});

Second, to sort the list, do the following:

IEnumerable<Checkit> query = Lst_Ch.OrderBy(l => l.ID).ToList();

The variable query is now your sorted list, which you can use elsewhere, loop through, etc.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
0

You can

  • Use Linq to sort it and create a new List<T>. Something like:

    List<Foo> unsortedList = CreateAnUnorderedList() ;
    List<Foo> sortedList   = unsortedList.OrderBy( x => x.DateCreated )
                                         .ThenBy(  x => x.Id          )
                                         .ToList()
                                         ; 
    
  • You can sort it in place. Something like:

    List<Foo> myList = CreateAnUnorderedList() ;
    myList.Sort( (x,y) => x.Id < y.Id ? -1 : x.Id > y.Id ? +1 : 0 ) ;
    

Alternatively, you can use an ordered collection, like SortedList<T>.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135