Two classes exist for sorting collections. One is for already created collections. The other is for collections to inherit from.
The CollectionSorter allows you to sort collections by specifying one or more properties to sort by. You can also specify the sort order.
In this example, we have a Person object that contains an Age property.
ArrayList personArray = new ArrayList();
personArray.Add(new Person("Joey", 21));
personArray.Add(new Person("Johnny", 30));
personArray.Add(new Person("Marky", 28));
personArray.Add(new Person("C.J.", 28));
personArray.Add(new Person("Joey", 25));
personArray.Add(new Person("Dee Dee", 33));
personArray.Sort(new CollectionSorter("Age"));
// 25-year old Joey is now the second in the collection
// after 21-year old Joey
Assert.AreEqual("Joey", (personArray[1] as Person).Name);
Assert.AreEqual(25, (personArray[1] as Person).Age);
In this example, we have a Person object that contains a Name and Age property.
ArrayList personArray = new ArrayList();
personArray.Add(new Person("Joey", 21));
personArray.Add(new Person("Johnny", 30));
personArray.Add(new Person("Marky", 28));
personArray.Add(new Person("C.J.", 28));
personArray.Add(new Person("Joey", 25));
personArray.Add(new Person("Dee Dee", 33));
personArray.Sort(new CollectionSorter("Name", "Age"));
// 25-year old Joey is now the fourth in the collection
// after 21-year old Joey
Assert.AreEqual("Joey", (personArray[3] as Person).Name);
Assert.AreEqual(25, (personArray[3] as Person).Age);
You can specify whether to sort ASC or DESC. To do this, pass in an array of the fields and the sort order which is specified by a boolean.
true = DESC
false = ASC
Constructor
public CollectionSorter(string[] fields, bool[] descending)
Example
Person[] personArray = this.GetPersonArray();
Assert.AreEqual("Johnny", personArray[1].Name);
// Sort by Name ASC, Age DESC
Array.Sort(personArray, new CollectionSorter(
new string[]{"Name","Age"},
new bool[]{false,true}
));
Assert.AreEqual("C.J.", personArray[0].Name);
Assert.AreEqual("Joey", personArray[2].Name);
Assert.AreEqual(25, personArray[2].Age);
Assert.AreEqual("Joey", personArray[3].Name);
Assert.AreEqual(21, personArray[3].Age);
SortableCollectionBase is intended to serve as the base class for collections that need advanced sorting capabilities.
Inheriting from SortableCollectionBase is straight forward. All you need to do is implement the indexer and Add() method. Below is an example.
public class PersonCollection : Adapdev.Collections.SortableCollectionBase
{
public Person this[int index]
{
get
{
return this.List[index] as Person;
}
set
{
this.List[index] = value;
}
}
public void Add(Person item)
{
if (!this.List.Contains(item))
this.List.Add(item);
}
}In this example, we have a Person object that contains an Age property.
PersonCollection personArray = new PersonCollection();
personArray.Add(new Person("Joey", 21));
personArray.Add(new Person("Johnny", 30));
personArray.Add(new Person("Marky", 28));
personArray.Add(new Person("C.J.", 28));
personArray.Add(new Person("Joey", 25));
personArray.Add(new Person("Dee Dee", 33));
personArray.Sort("Age");
// 25-year old Joey is now the second in the collection
// after 21-year old Joey
Assert.AreEqual("Joey", (personArray[1] as Person).Name);
Assert.AreEqual(25, (personArray[1] as Person).Age);
In this example, we have a Person object that contains an Age property.
PersonCollection personArray = new PersonCollection();
personArray.Add(new Person("Joey", 21));
personArray.Add(new Person("Johnny", 30));
personArray.Add(new Person("Marky", 28));
personArray.Add(new Person("C.J.", 28));
personArray.Add(new Person("Joey", 25));
personArray.Add(new Person("Dee Dee", 33));
personArray.Sort("Name","Age");
// 25-year old Joey is now the fourth in the collection
// after 21-year old Joey
Assert.AreEqual("Joey", (personArray[3] as Person).Name);
Assert.AreEqual(25, (personArray[3] as Person).Age);
You can specify whether to sort ASC or DESC. To do this, pass in an array of the fields and the sort order which is specified by a boolean.
true = DESC
false = ASC
Constructor
public CollectionSorter(string[] fields, bool[] descending)
Example
PersonCollection personArray = new PersonCollection();
personArray.Add(new Person("Joey", 21));
personArray.Add(new Person("Johnny", 30));
personArray.Add(new Person("Marky", 28));
personArray.Add(new Person("C.J.", 28));
personArray.Add(new Person("Joey", 25));
personArray.Add(new Person("Dee Dee", 33));
Assert.AreEqual("Johnny", personArray[1].Name);
// Sort by Name ASC, Age DESC
Array.Sort("Name, Age DESC");
Assert.AreEqual("C.J.", personArray[0].Name);
Assert.AreEqual("Joey", personArray[2].Name);
Assert.AreEqual(25, personArray[2].Age);
Assert.AreEqual("Joey", personArray[3].Name);
Assert.AreEqual(21, personArray[3].Age);