Hi Jon
Here is the code. Please create a Console App to test this. You will see
that Items C and G gets swapped when executed in 1.1 and 2.0 respectively
Class 1
=================
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class2 c2 = new Class2();
c2.Test();
}
}
Class 2
=================
/// <summary>
/// Summary description for Class2.
/// </summary>
public class Class2
{
private ArrayList m_itemList = new ArrayList();
private Hashtable m_items = new Hashtable();
/// <summary>
/// The main entry point for the application.
/// </summary>
public void Test()
{
Item item1 = new Item("A",10.444444444);
Item item2 = new Item("B", 10.33333333333333);
Item item3 = new Item("C", 4.33333333333333);
Item item4 = new Item("E", 4.33333333333333);
Item item5 = new Item("F", 4.33333333333333);
Item item6 = new Item("G", 4.33333333333333);
Item item7 = new Item("D",30);
// Add items to ArrayList
m_itemList.Add(item1);
m_itemList.Add(item2);
m_itemList.Add(item3);
m_itemList.Add(item4);
m_itemList.Add(item5);
m_itemList.Add(item6);
m_itemList.Add(item7);
// Create a Hashtable with ItemCode and Quantity as Key and value
m_items.Add(item1.ItemCode, item1.Quantity);
m_items.Add(item2.ItemCode, item2.Quantity);
m_items.Add(item3.ItemCode, item3.Quantity);
m_items.Add(item4.ItemCode, item4.Quantity);
m_items.Add(item5.ItemCode, item5.Quantity);
m_items.Add(item6.ItemCode, item6.Quantity);
m_items.Add(item7.ItemCode, item7.Quantity);
m_itemList.Sort(new ItemComparer(m_items));
//m_itemList.Reverse();
foreach(Item item in m_itemList)
{
Console.WriteLine(item.ItemCode.ToString());
}
Console.ReadLine();
}
}
class ItemComparer : IComparer
{
private Hashtable m_items = null;
public ItemComparer(Hashtable items)
{
m_items = items;
}
public int Compare(object x, object y)
{
if (! (x is Item && y is Item) )
{
throw new ArgumentException("aDS");
}
else
{
return ((IComparable)
(m_items[((Item)x).ItemCode])).CompareTo(m_items[((Item)y).ItemCode]);
}
}
}
class Item
{
private double m_qty = 0;
private string m_itemCode = "";
public Item(string code, double Quantity)
{
m_itemCode = code;
m_qty = Quantity;
}
public string ItemCode
{
get { return m_itemCode; }
set { m_itemCode = value; }
}
public double Quantity
{
get { return m_qty; }
set { m_qty = value; }
}
}