• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting objects in an ArrayList

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
On top of spaghetti all covered in cheese, there was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic