• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays.sort and Collections.sort  RSS feed

 
Luciano Assis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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.
 
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
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
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • 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
  • 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: 423
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
LOL, sorry...

I was refering to you Leandro.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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).
 
Valentin Ivanov
Ranch Hand
Posts: 38
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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?
 
Oyvind Vevang
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
aaaa that is true
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!