tags:

views:

275

answers:

4

Possible Duplicate:
How to sort an array of FileInfo[] C#

Duplicate of How to sort an array of FileInfo[] C#

I have an array[] of objects, for certain reasons I cannot change it to a List<>, is it possible to sort on the array[] of objects, based on a value in 1 field of the object?

For example

arrayOfFruit[] fruit; 

fruit.sort(name);

thanks

+2  A: 

Use Array..::.Sort Method (Array, IComparer)

http://msdn.microsoft.com/en-us/library/aw9s5t8f.aspx

Grzenio
A: 

You can use a IComparer as a parameter of sort

Samuel Carrijo
A: 
void SomeMethod()
{
    Fruit fruits[] = GetArrayOfFruit();
    Array.Sort(fruits, FruitNameComparer);
}

int FruitNameComparer(Fruit lhsOfQEquals, Fruit rhsOfEquals)
{
    return String.Compare(lhsOfQEquals.Name, rhsOfEquals.Name);
}
Binary Worrier
+1  A: 

If you want a clean distinction of the selector and comparer, you can use this helper class. It's too bad you can't have a "class extension method" to give Array.Sort a set of overloads that take a selector.

public static class SelectingComparer<T>
{
    public static IComparer<T> Create<U>(Func<T, U> selector)
    {
        return new SelectingComparerImpl<U>(selector, null);
    }

    public static IComparer<T> Create<U>(Func<T, U> selector, IComparer<U> comparer)
    {
        return new SelectingComparerImpl<U>(selector, comparer.Compare);
    }

    public static IComparer<T> Create<U>(Func<T, U> selector, Comparison<U> comparison)
    {
        return new SelectingComparerImpl<U>(selector, comparison);
    }

    private class SelectingComparerImpl<U>
        : IComparer<T>
    {
        private Func<T, U> selector;
        private Comparison<U> comparer;

        public SelectingComparerImpl(Func<T, U> selector, Comparison<U> comparer)
        {
            if (selector == null)
                throw new ArgumentNullException();

            this.selector = selector;
            this.comparer = comparer ?? Comparer<U>.Default.Compare;
        }

        public int Compare(T x, T y)
        {
            return this.comparer(this.selector(x), this.selector(y));
        }
    }
}

In use:

public class Testing
{
    public void Foo()
    {
        FileInfo[] files = new FileInfo[30];
        // FILL THE FILE ARRAY
        Array.Sort(files, SelectingComparer<FileInfo>.Create(file => file.Name));
        Array.Sort(files, SelectingComparer<FileInfo>.Create(file => file.Name, StringComparer.OrdinalIgnoreCase));
    }
}
280Z28