如何按特定值对数组进行排序?

时间:2015-02-16 14:27:13

标签: java arrays

我想通过将字符串分组到特定类别来对输出进行排序。 正如您从我的代码中可以看出的那样,我在一个字符串/行中拥有类别团队名称,联盟,教练,分区和全职时间的值。我想按联盟名称排序每个字符串/行。例如,所有拥有联盟" Western"在字符串中将被组合在一起,并且所有团队都与联盟"东部"将被组合在一起。

代码:

Team.java:

public class Team 
{
private String name;
private String league;
private String[] coaches;
private String division;
private boolean fullTime;

public Team(String dataLine)
{
    String[] data = dataLine.split(",");
    this.name = data[0];

    this.coaches = getStringAsArray(data[1], ":");

    this.league = data[2];

    this.division = data[3];

    this.fullTime = data[4].equals("yes");      

}

public Team(){

}

private String[] getStringAsArray(String t, String delimiter)
{
    String[] result = t.split(delimiter);
    return result;
}

private String getArrayAsString(String[] coaches)
{

    coaches = this.getCoaches();
    String result = "";
    for(int i = 0; i<coaches.length; i++)
    {
        result += coaches[i] +" ";
    }
    result = result.trim();
    return result;
}

public void setName(String name)
{
    this.name = name;
}

public String getName()
{
    return this.name;
}

public void setCoaches(String coaches)
{
    this.coaches = getStringAsArray(coaches, ":");
}

public String getCoachesAsString()
{

    String result = getArrayAsString(coaches);
    return result;
}

public boolean isFullTime() {
    return fullTime;
}

public void setFullTime(boolean fullTime) {
    this.fullTime = fullTime;
}

public String getDivision() {
    return division;
}

public void setDivision(String division) {
    this.division = division;
}

public String[] getCoaches() {
    return coaches;
}

public void setCoaches(String[] coaches) {
    this.coaches = coaches;
}

public String getLeague() {
    return league;
}

public void setLeague(String league) {
    this.league = league;
}

@Override
public String toString() {
    return " Team name: " + name + ", League: " + this.league + ", Coaches: " + Arrays.toString(this.coaches) +  ", Division: " + this.division + ", Full Time: " + this.fullTime + "\n ";
}
}

StoreData.java:

public class StoreData {
public static ArrayList<Team> teams = new ArrayList<Team>();

public static String getTeams()
{
    String s = "";
    for(int i = 0; i < teams.size(); i++){
        s += teams.get(i);

    }
    return s;
}

public static ArrayList<Team> TeamListFromArray(String[] as)
{
     ArrayList<Team> teams = new ArrayList<Team>();
    // for( int i= 0 ; i < as.length; i++){
     for (String s: as){
        teams.add(new Team(s));
     }
     return teams;
}

}

FootballC.java:

public class FootballC {
 public static void main(String[] args)
{
    ReadCsv junk = new ReadCsv();
    junk.readCsv();
    System.out.println(StoreData.getTeams());
}
}

ReadCSV.java:

public class ReadCsv {

public void readCsv() {

    String csvFileToRead = "H:/Desktop/FootballRepo/TestData/football_teams_phase1.csv";
    BufferedReader br = null;
    String line = "";

    try {

        br = new BufferedReader(new FileReader(csvFileToRead));
        int i = 0;
        while ((line = br.readLine()) != null) {
            Team one = new Team(line);
            if(i > 0){
                 StoreData.teams.add(new Team(line));
            }else{
               i++;
            }
        }
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

public static ArrayList<Team> getTeams() {
    return StoreData.teams;
}

public static void setTeams(ArrayList<Team> teams) {
    StoreData.teams = teams;
}
}

3 个答案:

答案 0 :(得分:0)

这样的事情:

final String[] input = /* get input from anywhere */;
Arrays.sort(input, new Comparator<String>() {
    @Override
    public int compare(String a, String b) {
        String[] dataA = a.split(",");
        String leagueA = dataA[2];
        String[] dataB = b.split(",");
        String leagueB = dataB[2];
        return leagueA.compareTo(leagueB);
    }
});

它甚至可以在创建相应对象之前对您的团队进行排序。

答案 1 :(得分:0)

假设team.league不为空

排序:

teams.stream()
        .sorted(Comparator.comparing(Team::getLeague))
        .forEach(System.out::println);

分组:

teams.stream()
        .collect(Collectors.groupingBy(Team::getLeague))
        .forEach((l,t) -> System.out.println("League: " + l + ", Teams: " + t));

答案 2 :(得分:0)

获得团队列表后,您可以按如下方式对对象进行分组:

{
    public static Map<String, List<Team>> makeGroup(List<Team> teams) {
        Map<String, List<Team>> teamMap = new HashMap<String, List<Team>>();
        for (Team team : teams) {
            if (!teamMap.containsKey(team.getLeague())) {
                teamMap.put(team.getLeague(), new ArrayList<Team>());
            }

            List<Team> list = teamMap.get(team.getLeague());
            list.add(team);
        }
        return teamMap;
    }
}

此外,如果您想对团队进行排序,可以按照上面提到的MinecraftShamrock进行排序。