Results 1 to 4 of 4
Thread: Sorting an ArrayList
- 02-13-2010, 12:41 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 1
- Rep Power
- 0
{SOLVED} Sorting an ArrayList
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:Java Code:import java.util.ArrayList; import java.util.Collections; //create generic MyList class with type parameter T final class MyList<T extends Number>{ //create ArrayList of type T ArrayList<T> list = new ArrayList<T>(); //create an add method that adds argument of type T to ArrayList public void add( T new_element ){ list.add( new_element ); } //create method 'largest' that returns largest value of 'list' public T largest(){ Collections.sort( list ); //must return largest value after sorting } //create method 'smallest' that returns smallest value of 'list' public T smallest(){ Collections.sort( list ); //must return smallest value after sorting } } public final class GenericsHW{ public static void main(final String[] args) { MyList<Number> list = new MyList<Number>(); list.add( new Integer( 10 ) ); list.add( new Double( 1.2 ) ); } }
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.Java Code:/MyDev/java/HW3/GenericsHW.java /MyDev/java/HW3/GenericsHW.java:30: cannot find symbol symbol : method sort(java.util.ArrayList<T>) location: class java.util.Collections Collections.sort( list ); ^ MyDev/java/HW3/GenericsHW.java:36: cannot find symbol symbol : method sort(java.util.ArrayList<T>) location: class java.util.Collections Collections.sort( list ); ^
Last edited by flesh-bound-book; 02-13-2010 at 02:33 AM. Reason: solved
- 02-13-2010, 04:42 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
You are trying to use the sort() method declared as
public static <T extends Comparable<? super T>> void sort(List<T> list)
The important bit about that generic method is "T extends Comparable". Now your T extends Number and ... Number is not Comparable! On the face of it this is a bit weird because what are numbers if not comparable things?
The other form of sort() takes an explicit comparator argument:
Java Code:final class MyList<T extends Number>{ Comparator<T> comp = new Comparator<T>() { public int compare(T first, T second) { return /* your code goes here */; } }; // ... public T largest() { // this is OK; the sort will use comp to compare the T instances Collections.sort( list, comp ); return /* etc */; } }
Now all you have to do is fill in the comparator.
- 02-13-2010, 04:50 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
D@mn: I didn't notice it was solved...
Anyway, the fact that Oracle's designers didn't make Number comparable would make me nervous if I were trying to sort a list of them. So I'm curious about what the solution is.
- 02-13-2010, 12:20 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Well, my first uni assignment this year involved sorting our custom classes (12 attributes), by every possible attribute, and by multiple attributes at the same time, we had to write up our own comparison logic and about 10 sorting methods, also written by hand. So in retrospect, don't do things the "code monkey" way, and try to find some method that was already implemented, but try to think up your own.
Similar Threads
-
Sorting question !!!
By alrebatsd in forum New To JavaReplies: 5Last Post: 06-18-2009, 11:02 AM -
sorting
By jot321 in forum New To JavaReplies: 18Last Post: 10-02-2008, 10:30 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
sorting problem...
By mark-mlt in forum New To JavaReplies: 4Last Post: 04-17-2008, 02:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks