473,326 Members | 2,133 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,326 software developers and data experts.

Sorting objects placed in an ArrayList

Hi,

My question is that when you place objects in an ArrayList, is there
any library function that can sort them based on different fields of
the object. I tried using

Containers.sort(myList);

where myList is the name of my list.

Thanx,
Huzefa
Jul 17 '05 #1
2 51502
While it was 12/9/03 9:45 am throughout the UK, Huzefa sprinkled little
black dots on a white screen, and they fell thus:
Hi,

My question is that when you place objects in an ArrayList, is there
any library function that can sort them based on different fields of
the object.
Look up Collections.sort in the docs.
I tried using

Containers.sort(myList);

where myList is the name of my list.


And what happened when you tried it?

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

Jul 17 '05 #2
Use Collections.sort(List list, Comparator c)

Just create a comparator for the fields you want to sort on. Look at
the api doc for the Comparator class. The other way to do it would be
to have your class implement Comparable. That would define it's
"natural ordering" or sort order without using a Comparator.
Comparators are more usefull if you ever may need to sort by more than
one field or if there is no distict field which should constitue the
natural ordering of the objects in the List.

Here is a sample Comparator:

class Subject_MessageComparator implements Comparator
{
public int compare(Object messageWrapper1, Object
messageWrapper2)
{
try
{
String messageSubject1 =
((MessageWrapper)messageWrapper1).getSubject();
String messageSubject2 =
((MessageWrapper)messageWrapper2).getSubject();

if (messageSubject1 == null)
return -1;
else
return messageSubject1.compareTo(messageSubject2);
}
catch (Exception e)
{
return 0;
}
}
}
-Eric Dalquist

ho**@cryptomail.org (Huzefa) wrote in message news:<85*************************@posting.google.c om>...
Hi,

My question is that when you place objects in an ArrayList, is there
any library function that can sort them based on different fields of
the object. I tried using

Containers.sort(myList);

where myList is the name of my list.

Thanx,
Huzefa

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
7
by: ryba | last post by:
Hello I'm sorry for mistakes - my English isn't very well. I've got the problem with sorting objects in ArrayList. If I put there only strings, Sort method works great, but it doesnt work when I...
3
by: Arjen | last post by:
Hi, public class test { public ArrayList myList = new ArrayList(); } test myTest = new test(); myTest.myList.Add(<someobject>); This results in an error.
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: shahidasalavoodeen | last post by:
How can I sort objects in an arraylist based on a particular field of the object. my arraylist has a collection of objects. each object has one of the fields as name. now i want to sort the objects...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.