Using Generics To See If A List Is Ordered.

I had some pretty simple code that checked if an array of integers was in ascending order.  I needed to check a second list to see if it was also in ascending order.  The only problem was that is was an array of doubles.  The logic is identical for both lists.  The only difference is the type of data acted on.  This is where generics shine!

I pulled the old code into its own static method and used generics so I could use it on any array of objects that are comparable (implement IComparable):

private static bool IsAscending<T>(T[] values) where T : IComparable
{
    for (int i = 0; i < values.Length - 1; i++)
    {
        if (values[i].CompareTo(values[i + 1]) > 0)
            return false;
    }

    return true;
}

Technorati tags: ,
Published Friday, February 02, 2007 5:59 PM by PSteele

Comments

# re: Using Generics To See If A List Is Ordered.

It would be slightly more general if you declared the param as IList<T> instead of T[].

Friday, February 02, 2007 6:58 PM by kevindente

# re: Using Generics To See If A List Is Ordered.

The only thing missing is some 3.0 extension method goodness.

Friday, February 02, 2007 9:50 PM by Jonathan Allen

# re: Using Generics To See If A List Is Ordered.

Ah yes -- excellent point Kevin.  Thanks!

Friday, February 02, 2007 9:58 PM by PSteele

# re: Using Generics To See If A List Is Ordered.

Good example

Saturday, February 03, 2007 3:44 AM by vikram

# re: Using Generics To See If A List Is Ordered.

You should also constrain T to IComparable<T>, instead of IComparable. This will eliminate boxing when calling IComparable.CompareTo(object).

Monday, February 05, 2007 12:37 AM by Krzysztof Cwalina

# re: Using Generics To See If A List Is Ordered.

If the first element of the array/IList is null/Nothing you will get a NullReferenceException in your example code.

Monday, February 05, 2007 7:44 AM by Jason

# re: Using Generics To See If A List Is Ordered.

(sorry about sending you through a bunch of links, but I don't like repeating myself on the web...)

I just wrote a variation of this which allows mere IEnumerables to be used.  That code is described here: http://community.strongcoders.com/forums/ShowThread.aspx?PostID=1724

That in turn, is based on an article I wrote on my blog, which is here:http://honestillusion.com/blogs/blog_0/archive/2007/02/05/c-code-adding-skip-first-to-foreach.aspx

Monday, February 05, 2007 3:41 PM by James Curran

# re: Using Generics To See If A List Is Ordered.

>> If the first element of the array/IList is null/Nothing you will get a NullReferenceException in your example code.

However, before we can deal with that, we have to decided HOW we are going to deal with it.

Is a null lower than every other value?  Higher? Should a null anywhere in the list  mean that's declared "not ascending".  Should we allow a null on the ends but not in the middle? Repeated nulls on the ends?  Perhaps we should just ignore nulls in the middle?

Monday, February 05, 2007 3:46 PM by James Curran

# re: Using Generics To See If A List Is Ordered.

>>Is a null lower than every other value?

James,

According to the MSDN Library documentation for the IComparable.CompareTo method, all object instances are considered greater than null. Therefore, in an ascending ordered list, all the nulls will be at the beginning.

With regards to nulls being allowed in the list at all and multiple nulls being collapsed into a single, this is up to the particular implementation of IList, whether it be an array, List<T>, or something else entirely.

Tuesday, February 06, 2007 2:04 AM by Jason