• Post Reply Bookmark Topic Watch Topic
  • New Topic

sorting an arraylist with interface comparable<T>  RSS feed

 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • 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
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • 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:
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • 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: 56545
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You may need to create a Comparator object in order to sort backwards.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!