Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-08-2011, 08:57 PM   PM User | #1
gfence
New to the CF scene

 
Join Date: Mar 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
gfence is an unknown quantity at this point
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
    }

}
gfence is offline   Reply With Quote
Old 03-08-2011, 09:34 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:41 AM.


Advertisement
Log in to turn off these ads.