JeffDrummond
asked on
How to sort an IList of custom objects?
In my C# project, I have a typed IList of custom objects: IList<MyObject>.
I need to sort the IList based on a specific property of
my object. How can I do that?
I need to sort the IList based on a specific property of
my object. How can I do that?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try the following code. Student is the comparable custom object. Hope this helps you.
/*
* Created by SharpDevelop.
* User: Sajeewaa
* Date: 10/28/2008
* Time: 6:35 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections;
using System.Collections.Generic;
namespace ExpertsExchange
{
/// <summary>
/// Description of Student.
/// </summary>
public class Student : IComparer<Student>
{
private int _marks;
private string _name;
public int Marks {
get { return _marks; }
set { _marks = value; }
}
public string Name {
get { return _name; }
set { _name = value; }
}
public Student()
{}
public Student(int marks, string name)
{
this._marks = marks;
this._name = name;
}
/// <summary>
/// Compare Student object based on the marks.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int Compare(Student x, Student y)
{
return x.Marks.CompareTo(y.Marks);
}
}
}
// Test method
public static void SortStudents()
{
List<Student> students = new List<Student>();
students.Add(new Student(75, "Jason"));
students.Add(new Student(58, "Neomi"));
students.Add(new Student(45, "Jack"));
students.Add(new Student(80, "Horianna"));
students.Add(new Student(35, "Tom"));
Student comp = new Student();
students.Sort(comp);
foreach (Student item in students)
{
Console.WriteLine(item.Name);
}
}
ASKER
The following code sorts on the first property, but I am not quite sure from the comments above how to sort on a second or third property. Can you add to the code below and show me how to additionally sort based on a property called Type?
if (myList.Count > 1)
{
((List<DetailView>)myList) .Sort(dele gate(Detai lView td1, DetailView td2) { return td1.Description.CompareTo( td2.Descri ption); });
}
Thanks!
if (myList.Count > 1)
{
((List<DetailView>)myList)
}
Thanks!
ASKER
BTW, myList in the example is an IList, that's why I am typing it to a List, in order to get to the Sort method.
do you mean that you want to sort on one property and then on another consecutively? can you give a small example of data and the results that you would expect to see?
ASKER
Yes, it be like an ORDER BY in SQL, such as:
ORDER BY Description, Type;
ORDER BY Description, Type;
you originally asked how to sort on A property -- that's all this will do --
you will probably have to go with a comparer derived class to get the "order by" functionality, not quite sure how you would do that off the top of my head. i will try to work on it -- maybe in the meantime someone else will pop up with the answer.
you will probably have to go with a comparer derived class to get the "order by" functionality, not quite sure how you would do that off the top of my head. i will try to work on it -- maybe in the meantime someone else will pop up with the answer.
ASKER
This code is working correctly for me. It types myList (an IList) to a List
object to gain access to the List.Sort method. The first level of comparison
is on the Description property. If the values are equal, it goes to the second
level of comparison on the Type property.
if (myList.Count > 1)
{
((List<DetailView>)myList) .Sort(dele gate(Detai lView d1, DetailView d2)
{
if (d1.Description.Equals(d2. Descriptio n))
{
return d1.Type.CompareTo(d2.Type) ;
}
else
{
return d1.Description.CompareTo(d 2.Descript ion);
}
});
}
Thanks for your help.
object to gain access to the List.Sort method. The first level of comparison
is on the Description property. If the values are equal, it goes to the second
level of comparison on the Type property.
if (myList.Count > 1)
{
((List<DetailView>)myList)
{
if (d1.Description.Equals(d2.
{
return d1.Type.CompareTo(d2.Type)
}
else
{
return d1.Description.CompareTo(d
}
});
}
Thanks for your help.
ASKER
Together, both answers provided the information I needed.
cool
Glad i could a part of the solution.
Cheers.
Cheers.
http://geekswithblogs.net/paulwhitblog/archive/2006/05/08/77581.aspx has an example