1

我想对一些数据进行排序。目前,数据存储在地图中。我知道,我无法按值对地图中的数据进行排序。我这样计算足球赛程:

团队名称、G+、G-、P

我想先按 P 排序,然后按 G+,然后按 G-。

每个 k,v 都在这样的地图中:

map.put(e.getString("team_id"), 0);    
map.put(e.getString("team_id")+"G+", 0);
map.put(e.getString("team_id")+"G-", 0);

我知道数据结构真的很糟糕!我认为最好将值放入 aCollection中以执行 a collection.sort。但是我该怎么做呢?

这是我的代码(代码工作正常,但未排序且编码错误):

HashMap<String, Integer> map = new HashMap<String, Integer>();
HashMap<String, String> tab = new HashMap<String, String>();

for(int i=0; i<teams.length(); i++){                
    JSONObject e = teams.getJSONObject(i);
    //get TeamID
    map.put(e.getString("team_id"), 0);
    //Goals +
    map.put(e.getString("team_id")+"G+", 0);
    //Goals -
    map.put(e.getString("team_id")+"G-", 0);
    //standings.add(map);
    //Log.e("Team7", String.valueOf(map.get("7")));
    //Log.e("Team7", e.getString("team_id"));
}

for(int i=0; i<matchdata.length(); i++){
    JSONObject e = matchdata.getJSONObject(i);
    //calculate Points
    int myVarGoal1 = Integer.valueOf(e.getString("points_team1"));
    int myVarGoal2 = Integer.valueOf(e.getString("points_team2"));
    if ((myVarGoal1) > (myVarGoal2)){
        myPoint1 = 3;
        myPoint2 = 0;
    }
    if ((myVarGoal1) < (myVarGoal2)){
        myPoint1 = 0;
        myPoint2 = 3;
    }
    if ((myVarGoal1) == (myVarGoal2)){
        myPoint1 = 1;
        myPoint2 = 1;
    }
    int calc1 = (map.get(e.getString("id_team1")) + myPoint1);
    int calc2 = (map.get(e.getString("id_team2")) + myPoint2);
    map.put("id",  Integer.valueOf(i));
    map.put(e.getString("id_team1"), calc1);
    map.put(e.getString("id_team2"), calc2);

    //calculate Goals
    int calcGoal1 = (map.get(e.getString("id_team1")+"G+") + myVarGoal1);
    int calcGoal2 = (map.get(e.getString("id_team1")+"G-") + myVarGoal2);
    int calcGoal3 = (map.get(e.getString("id_team2")+"G+") + myVarGoal2);
    int calcGoal4 = (map.get(e.getString("id_team2")+"G-") + myVarGoal1);
    map.put(e.getString("id_team1")+"G+", calcGoal1);
    map.put(e.getString("id_team1")+"G-", calcGoal2);
    map.put(e.getString("id_team2")+"G+", calcGoal3);
    map.put(e.getString("id_team2")+"G-", calcGoal4);
    //standings.add(map);
    //Log.e("TeamID", e.getString("id_team1"));
    //Log.e("PointsTeam7", String.valueOf(map.get("7")));
    //Log.e("GaolsTeam7", String.valueOf(map.get("7G-")));
}

for(int i=0; i<teams.length(); i++){                
    JSONObject e = teams.getJSONObject(i);
    String myTeamID = e.getString("team_id");
    int Gdif = (map.get(myTeamID+"G+")) - (map.get(myTeamID+"G-"));
    tab.put(myTeamID, e.getString("team_name") +","+ map.get(myTeamID) +","+ (map.get(myTeamID+"G+")) +":"+ (map.get(myTeamID+"G-")) +" "+ Gdif);
    //Log.e("Team7", String.valueOf(tab.get("7")));
    //Log.e("Team7", e.getString("team_id"));
    strGoals+="\n" + String.valueOf(tab.get(myTeamID));
}
4

2 回答 2

0

听起来您需要首先创建自己的类来将相关数据保存为一个对象。类的确切名称取决于数据是什么。也许SoccerTeamSoccerSchedule。创建此类后,您可以实现Comparable接口或创建Comparator定义排序顺序的对象。

于 2013-09-23T19:00:15.187 回答
0

我认为您正在寻找的是 aTreeMap和 a Comparator。你可以改用 aTreeMap吗?它就像 a 一样工作HashMap,但会自动为您排序您的密钥。然后你可以使用Comparator这样的:

Map<String, Integer> map = new HashMap<String, Integer>();

    ..... do stuff .....

TreeMap<String, Integer> treeMap = new TreeMap<String, Integer>(new Comparator<String>()
{
    @Override
    public int compare(String lhs, String rhs)
    {
        // return 1 if lhs > rhs
        // return 0 if lhs = rhs
        // return -1 if lhs < rhs
        if (lhs == null && rhs == null) return 0;
        if (lhs == null) return -1;
        if (rhs == null) return 1;

        if ((lhs.endsWith("P") && (rhs.endsWith("P")))
            || (lhs.endsWith("G+") && (rhs.endsWith("G+")))
            || (lhs.endsWith("G-") && (rhs.endsWith("G-"))))
        {
            return lhs.compareTo(rhs);
        }
        else if (lhs.endsWith("P"))
        {
            return -1;
        }
        else if (rhs.endsWith("P"))
        {
            return 1;
        }
        else
        {
            String lastLeftChar = lhs.substring(lhs.length()-1);
            String lastRightChar = rhs.substring(rhs.length()-1);

            return lastLeftChar.compareTo(lastRightChar);
        }
    }
});
treeMap.putAll(map);

// Now your treeMap is sorted by the keys!
于 2013-09-23T18:54:53.493 回答