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

Sorting an ArrayList

P: n/a
Hi !

I have Objects in my ArrayList
These Objects contain a String called "Name".
And i want that ArrayList to sort
it's objects using the Name element of each
object and not something else of the object ?!?
I think this is somehow accomplished using the
Comparer class, but i have not checked it out yet.

Please help !

Thanks, nick
Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Nick <ni**@winger.at> wrote:
I have Objects in my ArrayList
These Objects contain a String called "Name".
And i want that ArrayList to sort
it's objects using the Name element of each
object and not something else of the object ?!?
I think this is somehow accomplished using the
Comparer class, but i have not checked it out yet.


Use ArrayList.Sort(IComparer) where you write an implementation of
IComparer which compares two of your objects by name. Alternatively,
make your objects themselves implement IComparable in a similar way,
and just call ArrayList.Sort.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2

P: n/a
On Wed, 29 Oct 2003 05:11:57 -0800, Nick wrote:
Hi !

I have Objects in my ArrayList
These Objects contain a String called "Name".
And i want that ArrayList to sort


Instead of an ArrayList, use a SortedList. You can place the Name in the
key and it will be sorted by name.
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 22 '05 #3

P: n/a
In article <0a****************************@phx.gbl>, ni**@winger.at
says...
I have Objects in my ArrayList
These Objects contain a String called "Name".
And i want that ArrayList to sort
it's objects using the Name element of each
object and not something else of the object ?!?


Here's how I did it (I also have a pretty good typesafe ArrayList for
this class courtesy of 10*@100.com, if you'd like to see it):

public class QRow : IComparable, ICloneable
{
public int qId;
public int testId;
public string objective;
public int placement;

////////////////////////////////////////////////////////////
public QRow()
{
qId = 0;
testId = 0;
objective = "";
placement = -1;
}

////////////////////////////////////////////////////////////
public QRow(QRow other)
{
this.qId = other.qId;
this.testId = other.testId;
this.objective = other.objective;
this.placement = other.placement;
}

#region IComparable Members

public int CompareTo(object rhs)
{
QRow r = (QRow) rhs;
return this.placement.CompareTo(r.placement);
}

#endregion

#region ICloneable Members

public object Clone()
{
return new QRow(this);
}

#endregion
}

Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.