Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Sorting a list error

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Thanked 0 Times in 0 Posts

    Default Sorting a list error

    I am reading a file and sorting a list, and I cannot figure out why I am getting an error on line 15 that contains the following code
    	 Collections.sort(sortedContributorList, new Contributor());

    This is the error I keep getting:

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

    import java.util.*;
    import java.io.*;
    public class myhashTable {
     public static LinkedList<Contributor> sortedContributorList = new LinkedList<Contributor>();
    public myhashTable(){
    public void sortedLL(){
    	 Collections.sort(sortedContributorList, new Contributor());
    private static void readFile()
    {//opens a private method for reading the contributor.csv file
    	String fileLocation ="C:address/here.csv";
            //variable to hold the file 
            String readLine;//sets the variable to read the lines within the file
            BufferedReader csvBuffReader;//calls the reader to read the text from a character-input stream
            {//opens a try condition. In this instance we are trying to read a file
                csvBuffReader = new BufferedReader (new FileReader(fileLocation));//creates a new reader for reading the .csv file
                while ((readLine = csvBuffReader.readLine()) != null)//creates a while loop and condition for reading the line
                {//opens the while loop... the condition states, while there are lines to read, keep reading them, when the line is empty, stop reading
                    Contributor contributors = createContribList(readLine);//creates a new linked list/stack to store the contents of the read file
                    addToList (contributors);//as the file is being read, this places the information into a linked stack
                }//closes the while loop
            }//ends the try condition
            catch (IOException e)//presents a solution for handling an error if the try was unable to import the file or export data to a file
            {//opens the catch block
                System.out.println(e.toString());//if an error occurs, the error handler would print an error message to screen
            }//ends the catch block
            System.out.println("Reading the CSV file was successful.");//prints message to the screen
    public static Contributor createContribList (String line){
        final String LL = ", ";//sets the variable to read the line until a comma appears, then proceeds to call that string a token
        StringTokenizer lineToken;//breaks the lines of text into desired strings for enhanced readability
        int tokenNumber = 0;//sets and initializes the variable
        Contributor sortedList = new Contributor();//creates a new linked list/stack for storing information from the file
        lineToken = new StringTokenizer(line, LL);//creates a new tokenizer for breaking the lines of information into appropriate sections
                while(lineToken.hasMoreTokens()){ // while loop to set conditions to set data with tokennumber as long as it has more tokens
                    switch (tokenNumber){// sentinel switch to move tokenizer and set data in variables with data types
                        case 0: sortedList.setFirstName(lineToken.nextToken());
                        case 1: sortedList.setLastName(lineToken.nextToken());
                        case 2: sortedList.setCountry(lineToken.nextToken());
                        case 3: sortedList.setPhoneNumber(lineToken.nextToken());
                        case 4: sortedList.setContributorAmount(Double.valueOf(lineToken.nextToken()));
                        case 5: sortedList.setId(Integer.valueOf(lineToken.nextToken()));
                    }//ends the switch condition for placing tokenized data into the appropriate sections
                    tokenNumber++;//Increments the token number so that each token is read
                }//ends the while loop
                return sortedList;//returns the tokenized and switched information 
            }//ends the private method for storing the switched and tokenized data read from a file
    public static void addToList(Contributor LL)//method to add data to LL
    {//opens the private method
      sortedContributorList.add(LL);//add the data into each node of the linked list/stack  
    }//closes the private method
        public static void printSortedList(List<Contributor> lL)//prints info in the linked list/stack
    {//opens the private method
        Contributor currentSort;//sets the variable for the linked list
        for (int count=0;count<lL.size(); count++)//creates a loop for reading the linked list/stack. once the stack is empty the compiler stops reading
        {//opens the for loop
            currentSort = lL.get(count);//get the information
            System.out.println("Contributor # " + (count+1));//header
            System.out.println();//formatting readability
            currentSort.printContr();// prints nodes
        }//ends the for loop

  2. #2
    Senior Member
    Join Date
    Jul 2013
    Thanked 166 Times in 148 Posts

    Default Re: Sorting a list error

    Well, look at the error message:
    The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (LinkedList<Contributor>, Contributor)
    it is pretty straight forward.

    The "sort" method needs a List as first parameter and a Comparator as a second parameter.
    You try to give it a List and a Contributor. I dont know what a Contributor is supposed to be but it is obviously not a Comparator.

  3. The Following User Says Thank You to Cornix For This Useful Post:

    GregBrannon (June 12th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 778 Times in 763 Posts

    Default Re: Sorting a list error

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

Similar Threads

  1. Sorting list code
    By Nkarasjava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2012, 09:51 AM
  2. Sorting a Linked List
    By nWeid1 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 21st, 2012, 08:07 AM
  3. Singly Circular Linked List Error
    By clydefrog in forum Collections and Generics
    Replies: 7
    Last Post: March 5th, 2012, 08:17 PM
  4. Reading file of floats into array list and sorting into subsets
    By Skave in forum Collections and Generics
    Replies: 2
    Last Post: November 9th, 2011, 07:03 PM
  5. Sorting a doubly linked list
    By snufkin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 26th, 2010, 12:01 PM

Tags for this Thread