Array-sorting using generics and anonymous methods

Alternative 1 (type safe sorting using generics):

private int ComparePass(Pass x, Pass y)
{
    if (x.Starttid < y.Starttid) return -1;
    if (x.Starttid > y.Starttid) return 1;
    return 0;
}
:
:
    Pass[] results = ...;
    Array.Sort<Pass>(results, ComparePass);
:

Alternative 2 (type safe sorting using generics and an anonymous method):

Pass[] results = ...;
Array.Sort<Pass>(results,
    delegate(Pass x, Pass y)
    {
        if (x.Starttid < y.Starttid) return -1;
        if (x.Starttid > y.Starttid) return 1;
        return 0;
    });

Alternative 3 (type safe sorting using generics and an anonymous method using a default comparer):

Pass[] results = ...;
Array.Sort<Pass>(results, 
    delegate(Pass x, Pass y)
    {
        return Comparer.Default.Compare(x.Starttid, y.Starttid);
    });

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.