Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post How to sort Linked List?

    I need to know how to sort my linked list "teams" below.
    Code:
    package linkorderedlist;
    import java.util.*;
    /**
     *
     * @author jason.gladfelder
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
             LinkedList teams = new LinkedList();
             Scanner scannerObject= new Scanner (System.in);
             System.out.println("Input a team, then press enter, then its number of wins, then enter again.");
             System.out.println("Only 4 teams permmitted, input done when finished.");
             int teamwinA, teamwinB, teamwinC, teamwinD;
             String teamA= scannerObject.nextLine();
             teamwinA=scannerObject.nextInt();
             String teamB= scannerObject.nextLine();
             teamwinB=scannerObject.nextInt();
             String teamC= scannerObject.nextLine();
             teamwinC=scannerObject.nextInt();
             String teamD= scannerObject.nextLine();
             teamwinD=scannerObject.nextInt();
             BufferedReader reader = new BufferedReader(input);
             while (team!=done)
             {
              teams.add(teamA+teamwinA);
              teams.add(teamB+teamwinB);
              teams.add(teamC+teamwinC);
              teams.add(teamD+teamwinD);
             }
           Collections.sort(teams);
                for (String teamsh : teams) {
                System.out.println("Grade = " + grade1);
    
    
             }
            // TODO code application logic here
        }
    
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I won't figure out what all the compiler errors are supposed to be, so I'll just just answer your question.

    Use a generic LinkedList<String>. Then it will sort. Strings are Comparable by default, so no special Comparator needs to be written for it. Since you are combining data like names and wins and whatnots, you'd be better off with writing an object to deal with those and implement a Comparable yourself.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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