我想对一些数据进行排序。此时数据存储在地图中。我知道,我不能按值对地图中的数据进行排序。我计算了这样的足球赛程:
TeamName,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);
我知道数据结构非常糟糕!我认为最好将值设置为Collection
以执行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));
}
答案 0 :(得分:0)
我认为您要找的是TreeMap
和Comparator
。你可以转而使用TreeMap
吗?它的工作方式与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!
答案 1 :(得分:0)
听起来你需要先创建自己的类来将相关数据保存为一个单独的对象。类的确切名称取决于数据的内容。可能是SoccerTeam
或SoccerSchedule
。创建此类后,您可以实现Comparable
接口或创建定义排序顺序的Comparator
对象。