How to use the SortedList in C#, using Generics type objects
Suppose we have a simple class Employees, which implements the interface IComparable
Code:
protected class Employees : IComparable<Employees>
{
private string name = String.Empty;
private int age = 0;
private double salary = 0.0d;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public double Salary
{
get { return salary; }
set { salary = value; }
}
/// <summary>
/// The constructor of the class People
/// </summary>
/// <param name="n"></param>
/// <param name="a"></param>
/// <param name="s"></param>
public Employees(string n, int a, double s)
{
name = n;
age = a;
salary = s;
}
/// <summary>
/// Override the ToString() method
/// </summary>
/// <returns></returns>
override public string ToString()
{
return String.Format("{0:s}, {1:G} - Salary: {2:c}", name, age, salary);
}
/// <summary>
/// Override the CompareTo method,
/// compare name fields
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int CompareTo( Employees obj)
{
Employees otherPeople = obj as Employees;
if (otherPeople != null)
return this.name.CompareTo(otherPeople.name);
else
throw new ArgumentException("Object is not a Employees");
}
}
Create a EmployeesComparer class that implements the IComparer<int> interface
Code:
protected class EmployeesComparer : IComparer<int>
{
public int Compare(int x, int y)
{
return x.CompareTo(y);
}
}
Example of how to use the functions to manage the SortedList:
Code:
// Create an instance of a SortedList of Employees type
SortedList<int, Employees> employees = new SortedList<int, Employees>(new EmployeesComparer());
// Insert some objects of type "Employees" in the SortedList
employees.Add(5, new Employees("Yasmine", 32, 1800.00d));
employees.Add(9, new Employees("Gabrielle", 38, 2300.00d));
employees.Add(8, new Employees("Lynette", 35, 2200.00d));
employees.Add(4, new Employees("Asia", 33, 1900.00d));
employees.Add(1, new Employees("Daisy", 36, 2000.00d));
employees.Add(6, new Employees("Lorelayne", 39, 2100.00d));
employees.Add(7, new Employees("Jenny", 36, 2300.00d));
employees.Add(3, new Employees("Lisa", 34, 2600.00d));
employees.Add(2, new Employees("Ylenia", 37, 1700.00d));
// Print the contents of the SortedList
Response.Write("<h2>Content of SortedList:</h2>");
foreach (KeyValuePair<int, Employees> kvp in employees)
{
Response.Write(String.Format("ID = {0} [{1}]<br />", kvp.Key, kvp.Value.ToString()));
}
Here is the result for the sample, please note that items are added in the list with unordered ID, but the list stores them in order:
Content of SortedList:
ID = 1 [Daisy, 36 - Salary: $2,000.00]
ID = 2 [Ylenia, 37 - Salary: $1,700.00]
ID = 3 [Lisa, 34 - Salary: $2,600.00]
ID = 4 [Asia, 33 - Salary: $1,900.00]
ID = 5 [Yasmine, 32 - Salary: $1,800.00]
ID = 6 [Lorelayne, 39 - Salary: $2,100.00]
ID = 7 [Jenny, 36 - Salary: $2,300.00]
ID = 8 [Lynette, 35 - Salary: $2,200.00]
ID = 9 [Gabrielle, 38 - Salary: $2,300.00]