0

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.

5
  • Could you please share the code for the arrayList? Show us what it contains and what you need to achive. Commented Nov 11, 2014 at 5:06
  • Do all of these objects have a price? Do they share an interface or a base class that exposes that price property? Commented Nov 11, 2014 at 5:09
  • You have to implement IComparable to define you want to sort on price and optionally implement IComparer to pass to sort to tell the order of sorting. Commented Nov 11, 2014 at 5:24
  • ArrayList is effectively deprecated. Is there a reason why you have to use it? Commented Nov 11, 2014 at 5:36
  • Ok, now I have added few statements of the original code Commented Nov 11, 2014 at 8:52

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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();

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.