chathu
chathu

Reputation: 41

Sort ArrayList with custom comparison

I am trying to sort an ArrayList using c#. When the ArrayList contains comparable objects, it is possible to sort with using list.Sort() but I need to sort an ArrayList which contains non-comparable objects. For example, let's say the object is Ring and it has an attribute property Price. Then I need to sort the ArrayList to the price order. If is is possible to select ascending or descending that will more helpful. Thank You!

Blockquote

arrAtdMon = **(ArrayList)**hashTb[unixMon];

if (arrAtdMon != null) monCount = arrAtdMon.Count;

int[] arrayMax = { monCount, tueCount, wedCount, thuCount, friCount };

int maxValue = arrayMax.Max();

KidAttendance valMon = null; string monTagName = string.Empty;

Blockquote

above array list is to be sorted it self.

Upvotes: 0

Views: 1966

Answers (4)

Rahul Singh
Rahul Singh

Reputation: 21795

You can do this by implementing IComparer interface:-

public class Ring : IComparer
{
    public decimal Price { get; set; }

    public int Compare(object x, object y)
    {
        return ((Ring)x).Price.CompareTo(((Ring)y).Price);
    }
}

Working Fiddle.

Upvotes: 3

Chief Wiggum
Chief Wiggum

Reputation: 2934

Assuming that these objects share a base class or an interface with the price property you should be able to do something like this:

// Base class with price property, could also be an shared interface
public abstract class Product
{
    public decimal Price{get;set;}
}

public class Ring : Product
{

}
public class Bag : Product
{

}

// Some test data
var myUnsortedArray = new Product[]{new Ring{Price = 1.2m}, new Bag{Price=2.5m}};

// Easy sort with LINQ
var sortedProducts = myUnsortedArray.OrderBy(p => p.Price).ToArray();
var sortedProductsDescending = myUnsortedArray.OrderByDescending(p => p.Price).ToArray();

UPDATE

I just realised that the question is about ArrayLists and have the changed solution below:

// Some test data
var myUnsortedArrayList = new ArrayList{new Ring{Price = 1.2m}, new Bag{Price=2.5m}};

// Easy sort with LINQ
var sortedProducts = myUnsortedArrayList.OfType<Product>().OrderBy(p => p.Price).ToArray();
var sortedProductsDescending = myUnsortedArrayList.OfType<Product>().OrderByDescending(p => p.Price).ToArray();

Upvotes: 1

Peter Duniho
Peter Duniho

Reputation: 70652

First, you really should be using the List<T> class, not ArrayList. Doing so wouldn't solve your problem, but it would make the code less fragile and more easy to maintain.

As for the specific question, you want to do something like this…

Assume:

class Ring { public decimal Price { get; set; } }

Then:

ArrayList list = ...; // Initialized as some collection of Ring instances

list.Sort(Comparer.Create((r1, r2) => r1.Price.CompareTo(r2.Price)));

This creates a new Comparer instance using the Comparison<T> of (r1, r2) => r1.Price.CompareTo(r2.Price). That is, for each pair of objects being compared, compare the price of the first with the price of the second.

Upvotes: 2

Rhyeen
Rhyeen

Reputation: 127

To sort an set of objects, the object needs to be Comparable and you can set up the comparison you'd like in the CompareTo() method: IComparable information here

Upvotes: 0

Related Questions