4. Adapdev.Collections

Two classes exist for sorting collections. One is for already created collections. The other is for collections to inherit from.

4.1. CollectionSorter

The CollectionSorter allows you to sort collections by specifying one or more properties to sort by. You can also specify the sort order.

4.1.1. Sorting By One Property

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);

4.1.2. Sorting By Multiple Properties

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);

4.1.3. Specifying Sort Order

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);

4.2. SortableCollectionBase

SortableCollectionBase is intended to serve as the base class for collections that need advanced sorting capabilities.

4.2.1. Inheriting from SortableCollectionBase

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);
		}
	}

4.2.2. Sorting By One Property

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);

4.2.3. Sorting By Multiple Properties

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);

4.2.4. Specifying Sort Order

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);