20120209_SortingviaIComparerObjects

20120209_SortingviaIComparerObjects - Sorting an Array List...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Sorting an Array List using an IComparer object.doc Copyright © 2012 Kyle Lutes Page 1 of 3 How to sort an array list using a IComparer object (in class) Because the only thing different in sorting logic is how two items are compared to each other, the .NET framework collections (e.g. arrays, ArrayLists, Lists) have a built-in Sort method. All you need to do is write the code to do the compares. We really don't know, or care, what sorting algorithm the collection uses. Because we are working in an Object-Oriented Programming language, we must put this compare logic in a class and instantiate an object from it to do the comparisons. For example, to sort an array list containing references to objects instantiated from clsEmployee in ascending Salary order: 1 using System; 2 using System.Collections; 3 4 public class clsEmployeeComparer: IComparer 5 { 6 int IComparer.Compare(object object1, object object2) 7 { 8 clsEmployee empl1 = (clsEmployee)object1; 9 clsEmployee empl2 = (clsEmployee)object2;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

20120209_SortingviaIComparerObjects - Sorting an Array List...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online