i need to sort an ArrayList of objects WITHOUT using IComparable

D

Disccooker

and i'm lost. any ideas?

It's an object with a public property that is an Int. i receive an
Array of these objects and copy them to an arraylist. i need to sort
it based on this property and i cannot change the object model to
implement icomparable.
 
V

VJ

Can you derive , write your own class, with a Sort Method in it?.. in this
method you can implement your own sort algorithm

Vijay
 
N

Nicholas Paldino [.NET/C# MVP]

Create a class that implements IComparer, and pass an instance of that
to the Sort method.

Hope this helps.
 
D

Disccooker

that sounds like it might work, can you elaborate/provide examples? so
i would create a class called ProgramComparer, for example, which would
read the value of my property "MTP" and then i could simply call
m_Arraylist.sort(ProgramComparer) ?
 
B

Bjorn Abelli

that sounds like it might work, can you elaborate/provide examples? so
i would create a class called ProgramComparer, for example, which would
read the value of my property "MTP" and then i could simply call
m_Arraylist.sort(ProgramComparer) ?

Short example. Assuming the ArrayList holds references to instances of
Program:

=====================================

class Program
{
public int MTP;
}

=====================================

class ProgramComparer: IComparer
{
public int Compare(object o1, object o2)
{
Program b1 = (Program) o1;
Program b2 = (Program) o2;
return b2.MTP - b1.MTP;
}
}

=====================================

// Bjorn A
 
M

Marcus Andrén

that sounds like it might work, can you elaborate/provide examples? so
i would create a class called ProgramComparer, for example, which would
read the value of my property "MTP" and then i could simply call
m_Arraylist.sort(ProgramComparer) ?

The class below is a simple IComparer that can be used to sort a list
of integers in reverse order.

class ReverseIntComparer : IComparer<int>
{
public int Compare(int x, int y)
{
return y.CompareTo(x);
}
}

Here is the same comparer, but without generics

class ReverseIntComparer : IComparer
{
public int Compare(object x, object y)
{
int x1 = (object) x;
int y1 = (object) y;

return y1.CompareTo(x1);
}
}

To sort an arraylist of integers in reverse order I would simply do
something like this:

myArraylist.Sort(new ReverseIntComparer());

Here is a final example that sorts people by looking at their last
name first.

class Person
{
public string FirstName;
public string LastName;
}

class PersonComparer : IComparer<Person)
{
public int Compare(Person a,Person b)
{
int compareLastName = a.LastName.CompareTo(b.LastName);

if(compareLastName != 0)
return compareLastName;

// The same last name
return a.FirstName.CompareTo(b.FirstName);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top