aspose file tools*
The moose likes Beginning Java and the fly likes Sorting an Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting an Array" Watch "Sorting an Array" New topic
Author

Sorting an Array

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1391
Why does the following code throw a ClassCastException?

bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
It looks like dashlist is a array of arrays. Therefore, when you do sort(dashlist), you are actually trying to sort arrays and not Strings (The sort only works on one dimension). As it happens arrays are not Comparable and Arrays uses the this Comparable interface to sort. Therefore, it tries to cast an array to Comparable which gives you the exception.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1391
How then can I sort this array?
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
You will have to explain how you expect it to be sorted. There is no natural way of sorting.

How would you expect this array to be sorted?
I guess it will be up to you to implement the sorting mechanism yourself.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Originally posted by Steve Dyke:
How then can I sort this array?


You need to write a Comparator that compares two String arrays in the way you require. You can then use the Arrays.sort() method that takes a Comparator object as its second parameter.


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting an Array
 
Similar Threads
Why we live?
build.xml question
input type=file
Netscape7.1 and window.createPopup()
onload