• 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 an arraylist with interface comparable<T>

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so I have a class so


so if I should fill an Arraylist<Mytype> with objects Mytype, How would collection.sort(arraylist) know to sort my arraylist with the fields in my class?
does it use the first field to compare or the second or the third? how is that specied?..how can I change the sorting order to be descending or ascending? thank you
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You need to implement method compareTo() which is defined in interface Comparable by comparing the fields you want to compare.
Also, specify object type you want to compare, I'm refering to the first line of code:
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mike Vigor wrote:How would collection.sort(arraylist) know to sort my arraylist with the fields in my class?
does it use the first field to compare or the second or the third? how is that specied?..how can I change the sorting order to be descending or ascending? thank you



Well, to sort in the natural order, it will use your class' Comparable interface... basically, it will ask your class to compare (in the natural order), and that is defined by your class. You can make the natural order by the first, second, third, or a combination of all, and/or make it ascending or descending.

Henry
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read all about it in the Comparable interface, and in the Java™ Tutorials. Simply, if a class implements Comparable, you are saying it has a total ordering. That means for every pair of instances of that class, one of the following is always “true”:-
  • 1: Instance 1 is “greater than” Instance 2.
  • 2: Instance 1 is “not different in rank from” Instance 2.
  • 3: Instance 1 is “less than” Instance 2.
  • Obviously only one of those can be “true” at any one time. There is lots more in the Tutorials link I gave you.

    Comparable is a parameterised interface, so you shou‍ld always give a type, which actually makes writing the compareTo method easier.I have just written “something” because I cannot predict the details of how to calculate the value for compareTo, but the links I gave shou‍ld explain everything. In the second example, you don't have the cast, and there is no risk of passing the wrong type and suffering an Exception. You would have to consider what to do it null is passed.
    Go through the List interface, the Collections class and the Java™ Tutorials to see what you can find out about sorting. You can also create a Stream from any List (Java8 only), and sort that Stream.
     
    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
    You may need to create a Comparator object in order to sort backwards.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic