• 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

Arrays.sort and Collections.sort

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I've read on Kathy's book that an array or collection need to be classified to use the sort method.

Testing on my eclipse, I noticed that in case of sorting a not classified object, the method Arrays.sort throws ClassCastException at runtime, but the method Collections.sort gave me a Compilation error.

What's the difference between these methods?

Thanks.
 
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

Luciano Assis wrote:
What's the difference between these methods?



The java.util.Arrays sort() method is used to sort arrays. And the java.util.Collections sort() method is used to sort Lists.

Henry
 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Luciano Assis wrote:
What's the difference between these methods?



The java.util.Arrays sort() method is used to sort arrays. And the java.util.Collections sort() method is used to sort Lists.

Henry


lol Henry. I think he knows it. =)

Arrays.sort() gives ClassCastException because you try to compare elements of different types.
Collections.sort() gives compile error because the class Object doesn't implement Comparable.

Next time post the code that you are using.
 
Luciano Assis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh ok Marcelo.

But im both situations the object have to implements Comparator/Comparable right?

Thank's for the explanation.


 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luciano Assis wrote:Oh ok Marcelo.

But im both situations the object have to implements Comparator/Comparable right?

Thank's for the explanation.


Who is Marcelo?
 
Luciano Assis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LOL, sorry...

I was refering to you Leandro.
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luciano Assis wrote:Oh ok Leandro.

But im both situations the object have to implements Comparator/Comparable right?

Thank's for the explanation.


So. You can sort passing a Comparator or not. If you don't pass a comparator, then the list type and the array type must implement Comparable, otherwise you'll get compile error and runtime exception (ClassCastException), respectively.

Comparator is normally used when you want to have different ways to sort the same object. You'll probably implement this using an inner class, then you would invoke Arrays.sort(T[] a, Comparator<? super T> c) and Collections.sort(List<T> list, Comparator<? super T> c).
 
Ranch Hand
Posts: 38
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Leonardo wrote:If you don't pass a comparator, then the list type and the array type must implement Comparable, otherwise you'll get compile error



Hi Leonardo

actually compile error is not given just because Comparable is not implemented:
Did i misunderstood something?
 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Valentin Ivanov wrote:Did i misunderstood something?



I think so. See the entire quote.

Leonardo wrote:If you don't pass a comparator, then the list type and the array type must implement Comparable, otherwise you'll get compile error and runtime exception (ClassCastException), respectively.



(My italics.) For Collections.sort(), you get a compile error. For Arrays.sort(), you get a ClassCastException at runtime.
 
Valentin Ivanov
Ranch Hand
Posts: 38
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aaaa that is true
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic