aspose file tools*
The moose likes Beginning Java and the fly likes Sorting objects in an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting objects in an ArrayList" Watch "Sorting objects in an ArrayList" New topic
Author

Sorting objects in an ArrayList

James Teo
Greenhorn

Joined: Jan 11, 2011
Posts: 7
Hi everyone.
i have a FreqAskQn class with instance variables
question:String
answer : String

i have also declared a arraylist
ArrayList<FreqAskQn> myList = new ArrayList<FreqAskQn>();

how do i sort the list such that the question are in alphabetical order?
any web links or examples to help me code this will do just fine.
Thank You!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

java.lang.Comparable
java.util.Comparator

Take a pick. If you don't see the difference, SearchFirst. There have been several threads about the difference between the two.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Welcome to the Ranch

I shall send you to the Java™ Tutorials where you can read all about sorting, but for a List<String> you will probably end up using the appropriately-named method of the Collections class.
By the way, if you go through the String class API (click wherever the word String is underlined), you will find it has a static member which is a Comparator for case-insensitive sorting, should you need it.
James Teo
Greenhorn

Joined: Jan 11, 2011
Posts: 7
Rob Prime wrote:java.lang.Comparable
java.util.Comparator

Take a pick. If you don't see the difference, SearchFirst. There have been several threads about the difference between the two.


Thanks for willing to help. But it is possible for you to show me a example of how i could use any of this(I am really new in java)?
I am new to this website too and i was unable to search for an example. If there is already an example, could you give me a web link?

Thank you very much!
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3829

James Teo wrote:... But it is possible for you to show me a example of how i could use any of this(I am really new in java)?...

Have you checked the Java Collections tutorial pointed by Campbell above (I have added it here too as that URL seems to be broken)


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Welcome to JavaRanch.

Create a class that implements interface java.util.Comparator for FreqAskQn objects. That would look like this:

You'll have to write some code inside the method to compare the two objects. The method should return < 0 if first comes before second, 0 if they are equal, and > 0 if second comes before first.

Then you can use the method Collections.sort(list, comparator) to sort the list.

Try writing the code yourself, and let us know if you got it to work or not.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Vijitha Kumara wrote: . . . pointed by Campbell above (I have added it here too as that URL seems to be broken)
sorry about that; somehow the same link has been added twice, and that broke it.
James Teo
Greenhorn

Joined: Jan 11, 2011
Posts: 7
Jesper de Jong wrote:Welcome to JavaRanch.

Create a class that implements interface java.util.Comparator for FreqAskQn objects. That would look like this:

You'll have to write some code inside the method to compare the two objects. The method should return < 0 if first comes before second, 0 if they are equal, and > 0 if second comes before first.

Then you can use the method Collections.sort(list, comparator) to sort the list.

Try writing the code yourself, and let us know if you got it to work or not.


Thank you very much! It works now.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
I thought there were Strings in the List, which already implement Comparable<String>, in which case it would have been a really simple problemSorry if I was mistaken on that point.
But if you have a Question class, you will have to provide a Comparator<Question> or implement Comparable<Question> yourself, as people have already told you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting objects in an ArrayList