473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Sorting an ArrayList

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
3 1614
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: awiklund | last post by:
Hi, I am sorting an arrayList using a custom comparer, but it sort the element in a uncommon fashion. Take a look at this code. using System; namespace ConsoleApplication8
6
by: Bryon | last post by:
I need to sort an ArrayList of objects. I am unable to find a method for this. DO I need to role my own sorting? Or is there something like the qsort() function in C of old??? Thanks
2
by: Rob G | last post by:
Hello, I have a basic understanding of Objects, inheritance, polymorhpism, etc. I am new to VB.NET and I am having a problem figuring this out. I want to sort a CheckBoxList. Using the...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
16
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support...
2
by: Rob Meade | last post by:
Dear all, I have a class which contains an arraylist populated with other objects, for example: PrescriptionQueue - containing multiple instances of Prescription I have the need on my web...
16
by: Kittyhawk | last post by:
I would like to sort an Arraylist of objects on multiple properties. For instance, I have a Sort Index property and an ID property (both integers). So, the results of my sort would look like this:...
3
by: djp | last post by:
Hi I have to sort arraylist. I tried to do this using this page as a reference: http://www.java2s.com/Code/CSharp/Collections-Data-Structure/UseIComparer.htm I did it exactly the same way but...
12
by: Justin | last post by:
Ok, I give up. I can't seem to construct a decent (productive) way of sorting my arraylist. I have a structure of two elements: Structure TabStructure Dim TabName As String Dim FullFilePath...
4
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.