How to sort List<T> on two criteria at same time.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fastestindian
    New Member
    • Aug 2009
    • 74

    How to sort List<T> on two criteria at same time.

    Hi,
    I am working on the project where i show a list of Log Records.

    my list is as follows.

    Original list
    Id Time Event Details
    1 1 Error1 xyz
    2 1 Error3 xyz
    3 1 Error2 xyz
    4 1 Error1 xyz
    5 1 Error2 xyz


    I first create the custom object of (Id,Time,Event, Details) and value (1, 1, Error1, xyz).
    I call it TableRowElement s.

    like this i create List<TableRowEl ements>.

    Now I want to sort the record according to Event but while doing that i also need to see that Event are in order

    for example

    Final List should be

    Id Time Event Details
    1 1 Error1 xyz
    4 1 Error1 xyz
    3 1 Error2 xyz
    5 1 Error2 xyz
    2 1 Error3 xyz

    I am using Icompare for this .

    I am getting the values sorted but only on 1 field 'Event'. After sorting the list does not keep its order.

    I am not sure how to sort this list on 2 criterias. I am working on WinCE 6.0.

    Thanks,
    Sachin
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    If you are overriding the Compare function , you can decide explicitly what order they go in.

    Comment

    • fastestindian
      New Member
      • Aug 2009
      • 74

      #3
      Issue is not that.

      I am able to sort the record on the 'Event' names but after sorting it is not able to maintain the chronological order i.e. the order of the 'Id'.

      Id Time Event Details
      1 1 Error1 xyz
      4 1 Error1 xyz
      5 1 Error2 xyz
      3 1 Error2 xyz

      2 1 Error3 xyz

      it should be
      Id Time Event Details
      1 1 Error1 xyz
      4 1 Error1 xyz
      3 1 Error2 xyz
      5 1 Error2 xyz

      2 1 Error3 xyz

      i also want to know whether the sorting can be possible for two fields as we do in the database i.e. order by Event, Id

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        So in your compare function sort FIRST on the ID, then sort on the Event

        Comment

        Working...