Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default {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.

    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 ) );
        }
    
    }
    and the error messages I am getting are:

    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 );
    	                ^
    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.
    Last edited by flesh-bound-book; 02-13-2010 at 03:33 AM. Reason: solved

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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

  1. Sorting question !!!
    By alrebatsd in forum New To Java
    Replies: 5
    Last Post: 06-18-2009, 12:02 PM
  2. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 11:30 AM
  3. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  4. sorting problem...
    By mark-mlt in forum New To Java
    Replies: 4
    Last Post: 04-17-2008, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •