Generic List Sort Issue with Same data?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nidhi Agarwal
    New Member
    • Jan 2011
    • 1

    Generic List Sort Issue with Same data?

    I am creating a list (myEmployeeList ) as follows

    myEmployeeList. Add(new Employee(12, "Anand", 25));
    myEmployeeList. Add(new Employee(12, "Viral", 26));
    myEmployeeList. Add(new Employee(12, "Meet", 20));
    myEmployeeList. Add(new Employee(12, "Rahul", 28));

    Now when i do sort with respect to ID column

    myEmployeeList. Sort(Employee.I DComparison);

    The resultant sorted list will be like
    12 Meet 20
    12 Rahul 28
    12 Anand 25
    12 Viral 26

    Note : The list order is changing earlier it was (Anand,VIral,Me et,Rahul) now (Meet,Rahul,Ana nd,Viral)
    Ideally it should not sort as the ID column is same (12) in all rows.
    How can i avoide Sorting when the column data is same in all rows?
    Please Respond
    Thanks in Advance
    Last edited by Nidhi Agarwal; Jan 12 '11, 11:33 AM. Reason: making more clarified
  • prasad599
    New Member
    • Feb 2009
    • 24

    #2
    This link will help you understand the behavior of "Sort" method

    Sorts the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements.

    Comment

    Working...