mercredi 22 juin 2016

sorting a LinkedList in decreasing order by number of hits and then increasing by batting average [duplicate]

This question already has an answer here:

I'm trying to sort my LinkedList in decreasing order by number of hits and also by increasing order by batting average. I'm not entirely sure how to do this so I came here to ask you guys for any tips or ideas of how to do so, here is my code so far:

 package blah;

 import java.util.LinkedList;
 import java.util.Collections;

 public class Blah {

  public void displayPlayers() {

    LinkedList<BaseballPlayer> players = new LinkedList<>();

    players.add(new BaseballPlayer("blah blah", 68, 13, .308));
    players.add(new BaseballPlayer("some guy", 47, 13, .257));
    players.add(new BaseballPlayer("baseball ball", 58, 9, .251));
    players.add(new BaseballPlayer("loser winner", 36, 12, .197));
    players.add(new BaseballPlayer("Kobe Bryant", 65, 14, .284));
    players.add(new BaseballPlayer("Jo man", 51, 12, .234));
    players.add(new BaseballPlayer("Mike B", 75, 14, .291));
    players.add(new BaseballPlayer("Charlie B", 55, 6, .302));
    players.add(new BaseballPlayer("George S", 70, 14, .278));
    players.add(new BaseballPlayer("Chris D", 46, 13, .220));

    //sort the players in decreasing order by  number of hits
    // number of hits
    for (BaseballPlayer bp : players) 
    {
        collection.sort();

        System.out.println(bp);
    }
    System.out.println("");

    // sort the players in increasing order by their batting average

    for (BaseballPlayer bp : players) 
    {
       collection.sort();

        System.out.println(bp);
    }
    System.out.println("");
}

public static void main(String[] args) {
    Blah blah = new Blah();
    blah.displayPlayers();
}

class BaseballPlayer {

    private String name;
    private int hits;
    private int homeRuns;
    private double average;

    public BaseballPlayer(String name, int hits, int homeRuns, double average) {
        this.name = name;
        this.hits = hits;
        this.homeRuns = homeRuns;
        this.average = average;
    }

    public String toString() {
        return name + ": (" + hits + ", " + homeRuns + ", " + average + ")";
    }
}
}

Aucun commentaire:

Enregistrer un commentaire