• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting objects in an ArrayList

 
James Teo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15278
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic