473,545 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1622
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.co m>
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(objec t rhs)
{
QRow r = (QRow) rhs;
return this.placement. CompareTo(r.pla cement);
}

#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
1717
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
8181
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
5063
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 ArrayList seemed to make the most sense. When I use it I get the error: At least one object must implement IComparable. Here's the code that produces that...
19
25432
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 sort columns based on the column header the user has clicked in both Ascending and Descending formats.
16
6399
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 sorting (because it's ultimately being consumed in a GridView). I can't simply sort the ArrayList (because it only knows it's holding a bunch of...
2
4715
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 page to display this data which I have done, however, now I would like to sort it based on a data item/direction selected by the user from the web...
16
2744
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: sort index, id 1000,1 1000,2 1000,3 1001,1 1001,2
3
3116
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 unfortunately in opposite to the example from this link here it does not sort. I Create Nodes ( color, name ) and add them to nodeList in Graph class....
12
9744
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 As String End Structure
4
5311
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 (Sorts.java): import java.util.*; /**
0
7401
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7808
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.