Results 1 to 6 of 6
  1. #1
    andreiutz10 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Exclamation Collections sorting a list made of generic class objects

    Hi, I need to write a program that uses the Collections sort method in order to sort through an ArrayList with objects from a generic class.

    My generic class looks like:
    Java Code:
    public class Pair<F, S> {
    	public F firstObject;
    	public S secondObject;
    	
    	
    	public Pair(F a, S b) {
    	firstObject = a;
    	secondObject = b;		
    	}
    }
    I also have the Comparator classes ready.

    The problem is in the main method where I'm reading the values from a text file and it looks something like:

    Java Code:
    ArrayList<Pair> list = new ArrayList<Pair>();
    while(sc.hasNextLine())
    			list.add(new Pair(new Advisor(sc.next()), new Student(sc.next(), sc.nextDouble())));
    		
    		for(Pair e: list)
    			System.out.println(e);	
    	
    
    		Collections.sort(list, new AdvisorPairComparator());
    		
    		System.out.println("*** Sorted by advisor name  ***");
    		for(Pair e: list)
    			System.out.println(e);
    So I keep getting an error saying: "The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (ArrayList<Pair>, AdvisorPairComparator)"

    Any ideas?

    Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: Collections sorting a list made of generic class objects

    How is AdvisorPairComparator defined?

  3. #3
    andreiutz10 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Collections sorting a list made of generic class objects

    Java Code:
    import java.util.Comparator;
    
    
    public class AdvisorPairComparator implements Comparator<Advisor>{	
    	
    	@Override
    	public int compare(Advisor a1, Advisor a2) {
    		if(a1.getName().equals(a2.getName()))		
    		return 0;
    		else{
    			int ceil = a1.getName().length()>a2.getName().length()?a2.getName().length():a1.getName().length();
    			
    			for(int i=0; i < ceil; i++) {
    				char c1 = a1.getName().charAt(i);
    				char c2 = a2.getName().charAt(i);
    				
    				if(c1 != c2)
    					return (c1<c2)?-1:1;
    			}
    			
    			return a1.getName().length()>a2.getName().length()?1:-1;			
    		}
    	}
    	
    }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: Collections sorting a list made of generic class objects

    There are many undefined classes so the code won't compile.

    If the list contains Pair objects, how can the Comparator for Advisor work with it?

  5. #5
    andreiutz10 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Collections sorting a list made of generic class objects

    Ok, so I made some changes to the code and my main class looks like this:

    Java Code:
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Scanner;
    
    
    public class Driver {
    
    	
    	@SuppressWarnings("unchecked")
    	public static void main(String[] args) throws Exception{
    		
    		ArrayList<Pair> list = new ArrayList<Pair>();
    		Scanner sc = new Scanner(new File("data.txt"));
    		
    		while(sc.hasNextLine())
    			list.add(new Pair<Advisor, Student>(new Advisor(sc.next()), new Student(sc.next(), sc.nextDouble())));
    		
    		for(Pair<Advisor, Student> e: list)
    			System.out.println(e);	
    	
    
    	Collections.sort(list, new AdvisorPairComparator());
    		
    		System.out.println("*** Sorted by advisor name  ***");
    		for(Pair e: list)
    			System.out.println(e);	
    		
    		Collections.sort(list, new StudentNamePairComparator());
    		
    		System.out.println("*** Sorted by student name  ***");
    		for(Pair e: list)
    			System.out.println(e); 
    		
    		Collections.sort(list, new StudentGpaPairComparator());
    		
    		System.out.println("*** Sorted by student GPA  ***");
    		for(Pair<Advisor,Student> e: list)
    			System.out.println(e);	
    	
    	}
    }
    and my comparator class looks like this:

    Java Code:
    import java.util.Comparator;
    
    public class AdvisorPairComparator implements Comparator<Pair<Advisor, Student>>
    
    {	
    	@SuppressWarnings("rawtypes")
    	public int compare(Pair a1, Pair a2) {
    		if(((Advisor) a1.getF()).getName().equals(((Advisor) a2.getF()).getName()))
    		return 0;
    		else{
    			int ceil = (((Advisor) a1.getF()).getName().length()>(((Advisor)a2.getF()).getName().length())?((Advisor) a2.getF()).getName().length():((Advisor) a1.getF()).getName().length());
    			
    			for(int i=0; i < ceil; i++) {
    				char c1 = ((Advisor) a1.getF()).getName().charAt(i);
    				char c2 = ((Advisor) a2.getF()).getName().charAt(i);
    				
    				if(c1 != c2)
    					return (c1<c2)?-1:1;
    			}
    			
    			return ((Advisor) a1.getF()).getName().length()>((Advisor) a2.getF()).getName().length()?1:-1;			
    		}
    	}
    	
    }
    I keep getting an error in the main class on the Collections.sort lines saying:

    The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (ArrayList<Pair>, AdvisorPairComparator)

    Any help would be greatly appreciated.

    Thanks.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: Collections sorting a list made of generic class objects

    It will compile with these changes (plus some more)
    Java Code:
            Collections.sort(list, new AdvisorPairComparator<Pair>());
    
    
    
     class AdvisorPairComparator<T> implements Comparator<T>{    
        @Override
        public int compare(T a1, T a2) {

Similar Threads

  1. Generic objects and collections
    By robbie.26 in forum Advanced Java
    Replies: 2
    Last Post: 12-24-2011, 11:29 AM
  2. Issues with comparing two generic objects
    By FOUAD-SPIDER in forum Advanced Java
    Replies: 0
    Last Post: 05-06-2011, 04:39 AM
  3. Generic linked list
    By mina in forum New To Java
    Replies: 1
    Last Post: 03-17-2010, 02:35 AM
  4. Generic Collections---Dysfuntional Java
    By beezerbutt in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 07:37 PM
  5. No class made
    By ChuckLS in forum New To Java
    Replies: 1
    Last Post: 04-28-2009, 04:54 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
  •