How do I sort custom object using ArrayList?

Category: System, viewed: 544 time(s).

In this example we sort a custom object using ArrayList.Sort() method. To enable the ArrayList.Sort() method sort the object have to implement the IComparable interface.

Our custom class is the Book class. This class implements the IComparable interface that will cause the ArrayList to sort the objects based on the book's ISBN property.

using System;
using System.Collections;

namespace Kodecsharp.Example.System
{
    class IComparableExample
    {
        public static void Main(string[] args)
        {
            Book bookA = new Book("1-9339-8836-3");
            Book bookB = new Book("0-7645-4834-4");
            Book bookC = new Book("0-596-00339-0");
            Book bookD = new Book("0-596-00181-9");
            Book bookE = new Book("0-596-00376-5");

            ArrayList books = new ArrayList();
            books.Add(bookA);
            books.Add(bookB);
            books.Add(bookC);
            books.Add(bookD);
            books.Add(bookE);

            books.Sort();
            foreach (Book book in books)
            {
                Console.WriteLine(book);
            }

            Console.ReadLine();
        }
    }
}

The Book class is a simple class with four properties. The ISBN, Title, Author and publishYear. The IComparable interface made the Book class to implement the CompareTo(object o) method.

For this example we just delegate the CompareTo() implementation to the string object CompareTo() method. This method should return three type of values, a positive, zero or negative value when the compared object value is greater, equals or smaller respectively.

using System;

namespace Kodecsharp.Example.System
{
    class Book : IComparable
    {
        private string isbn;
        private string title;
        private string author;
        private int publishYear;

        public Book() {
        }

        public Book(string isbn) {
            this.ISBN = isbn;
        }

        public string ISBN {
            get {
                return isbn;
            }
            set {
                this.isbn = value;
            }
        }

        //
        // Other properties implemented here!
        //
 
        public int CompareTo(object o)
        {
            if (this.GetType() != o.GetType()) {
                throw new ArgumentException("Object types is not equals.");
            }

            Book that = (Book) o;
            return this.ISBN.CompareTo(that.ISBN);
        }

        public override string ToString() {
            return "Book [" + this.ISBN + "]";
        }
    }
}
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Our Friends

Statistics

Locations of visitors to this page