By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,216 Members | 1,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,216 IT Pros & Developers. It's quick & easy.

i need to sort an ArrayList of objects WITHOUT using IComparable

P: n/a
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.

Apr 4 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

<Di********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
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.

Apr 4 '06 #2

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

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Di********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
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.

Apr 4 '06 #3

P: n/a
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) ?

Apr 4 '06 #4

P: n/a

<Di********@gmail.com> wrote...
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
Apr 5 '06 #5

P: n/a
On 4 Apr 2006 14:17:18 -0700, Di********@gmail.com wrote:
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);
}
}

--
Marcus Andrén
Apr 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.