aspose file tools*
The moose likes Beginning Java and the fly likes [SOLVED] Sorting an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[SOLVED] Sorting an ArrayList" Watch "[SOLVED] Sorting an ArrayList" New topic
Author

[SOLVED] Sorting an ArrayList

jakie presllie
Greenhorn

Joined: Feb 12, 2010
Posts: 3
Hey, guys. I have a homework assignment due later tonight and am having a bit of trouble. I have to sort an array list as part of the assignment. My understanding is that must use Collections.sort, but I can't get that to work. I am not sure if that is due to my use of generics or what. Here is my current code.



and the error messages I am getting are:



I've looked at quite a few explanations of sorting arraylists, and they all achieve sorting with Collections.sort( ArrayList );, but this isn't working for me. They all used string values for sorting and I am using numbers, but it seems like Collections.sort would be able to sort values of the Number type. Any help would be great. Let me know if I left out something you need to know.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18124
    
  39

If you look at the JavaDoc for the Collections class, you will see that the sort() method that takes one parameter, takes a generic that is an upper bound of Comparable. The generic that you defined doesn't fulfill that requirement.

You have a few options... (1) you can create a new list that takes a comparable generic, and copy your elements over, (2) you can add an upper bound to your class to require that the type be comparable, or (3) you can use a comparator instead, as that sort method doesn't have that requirement.

The easiest of these options, is option 2... which means that you have to change your class as follows...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18124
    
  39

And further down your code, where you create a MyList<Number>.... Well, you can't do that. The Number class doesn't implement Comparable.

Henry
jakie presllie
Greenhorn

Joined: Feb 12, 2010
Posts: 3
Ah, thank you. That seems to have worked. Believe it or not, I did look at the javadoc; it's just that I don't really know how to read the documentation, or how to learn how to read and understand it. I saw



and thought I just inserted a list (I assumed arraylist would work because I saw it in other peoples' code) of type T. Now that I've read your post I notice the



beside it, but I didn't know what that meant, nor that I could do <T extends Number & Comparable<? super T>>.

I see that if you click "sort" it takes you to a more detailed explanation, but when I looked at it earlier I saw



and didn't understand that I could do the same thing to a class, where as here it was a method.

Thanks for you help. I think that fixes it.
Ben Logan
Greenhorn

Joined: Feb 13, 2010
Posts: 12
For resources on generics, there were a few good links in this thread, particularly the Angelika Langer site:

http://www.coderanch.com/t/480743/Beginning-Java/java/About-Generics#2158324

jakie presllie
Greenhorn

Joined: Feb 12, 2010
Posts: 3
Ah, great. I will take a look. Thank you very much for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [SOLVED] Sorting an ArrayList
 
Similar Threads
Comparator vs comparable
Need help finding assignments
Sorting Arrays
Collection sort
warning: [unchecked] unchecked method invocation