我必须按获得的金牌数量以降序对国家进行排序。如果有几个国家并列,我需要按银牌数量对并列的国家进行排序,然后是铜牌,如果还有平局,我需要按字母升序对它们进行排序。我已经编写了一个比它可能需要的复杂得多的程序,但它仍然返回字符串的无序列表。输入是按年份给出的,第一个国家是金牌得主,第二个是银牌,第三个是铜牌。例如:
public class MedalTable {
public static void main(String[] args){
String[] input = {"ITA JPN AUS", "KOR TPE UKR", "KOR KOR GBR", "KOR CHN TPE"};
generate(input);
}
public static void generate(String[] results) {
// you fill in this code
//"ITA JPN AUS", "KOR TPE UKR", "KOR KOR GBR", "KOR CHN TPE"
String[] countrystrings = setupstuff(results);
ArrayList<Integer> goldnums = new ArrayList<Integer>();
ArrayList<Integer> silvnums = new ArrayList<Integer>();
ArrayList<Integer> broznums = new ArrayList<Integer>();
for(int o = 0; o < countrystrings.length; o++){
goldnums.add(Integer.parseInt(Character.toString(countrystrings[o].charAt(4))));
silvnums.add(Integer.parseInt(Character.toString(countrystrings[o].charAt(6))));
broznums.add(Integer.parseInt(Character.toString(countrystrings[o].charAt(8))));
}
}
public static String[] setupstuff(String[] data){
Map<String, Integer> goldmap = new TreeMap<String, Integer>();
Map<String, Integer> silvermap = new TreeMap<String, Integer>();
Map<String, Integer> bronzemap = new TreeMap<String, Integer>();
ArrayList<String> goldlist = new ArrayList<String>();
ArrayList<String> silverlist = new ArrayList<String>();
ArrayList<String> bronzelist = new ArrayList<String>();
ArrayList<String> countries = new ArrayList<String>();
for (String i : data){
goldlist.add(i.substring(0, 3));
silverlist.add(i.substring(4, 7));
bronzelist.add(i.substring(8, 11));
}
populatemap(goldmap, goldlist);
populatemap(silvermap, silverlist);
populatemap(bronzemap, bronzelist);
countries = getuniquecountries(goldmap, silvermap, bronzemap);
String[] countrystrings = new String[countries.size()];
countrystrings = createmedalstring(countries, goldmap, silvermap, bronzemap);
Arrays.sort(countrystrings);
for(int v = 0; v < countrystrings.length; v++){
System.out.println(countrystrings[v]);
}
return countrystrings;
}
public static String[] createmedalstring(ArrayList<String> array, Map<String, Integer> gldmap,
Map<String, Integer> slvmap, Map<String, Integer> brzmap){
String[] thiscountry = new String[array.size()];
ArrayList<String> tempstring = new ArrayList<String>();
for (int j = 0; j < array.size(); j++){
thiscountry[j] = array.get(j);
}
String[] concatstuff = new String[thiscountry.length];
for (int k = 0; k < thiscountry.length; k++){
concatstuff[k] = thiscountry[k];
}
for (int i = 0; i < thiscountry.length; i++){
tempstring.add(thiscountry[i]);
if(gldmap.containsKey(thiscountry[i])){
tempstring.add(" " + gldmap.get(array.get(i).toString()));
}
else{
tempstring.add(" 0");
}
if(slvmap.containsKey(thiscountry[i])){
tempstring.add(" " + slvmap.get(array.get(i).toString()));
}
else{
tempstring.add(" 0");
}
if(brzmap.containsKey(thiscountry[i])){
tempstring.add(" " + brzmap.get(array.get(i).toString()));
}
else{
tempstring.add(" 0");
}
concatstuff[i] = tempstring.get(0) + tempstring.get(1) + tempstring.get(2) + tempstring.get(3);
tempstring.clear();
}
return concatstuff;
}
public static ArrayList<String> getuniquecountries(Map<String, Integer> gldmap,
Map<String, Integer> slvmap, Map<String, Integer> brzmap){
ArrayList<String> countries = new ArrayList<String>();
for(Entry<String, Integer> i : gldmap.entrySet()){
if(!countries.contains(i.getKey()))
countries.add(i.getKey());
}
for(Entry<String, Integer> j : slvmap.entrySet()){
if(!countries.contains(j.getKey()))
countries.add(j.getKey());
}
for(Entry<String, Integer> k : brzmap.entrySet()){
if(!countries.contains(k.getKey()))
countries.add(k.getKey());
}
return countries;
}
public static void populatemap(Map<String, Integer> map, ArrayList<String> array){
for (String i : array){
if(map.containsKey(i)){
map.put(i, map.get(i) + 1);
}
else{
map.put(i, 1);
}
}
//System.out.println(map);
}
}
这将返回:
澳大利亚 0 0 1 中国 0 1 0 英国 0 0 1 意大利 1 0 0 日本 0 1 0 韩国 3 1 0 TPE 0 1 1 UKR 0 0 1
但是,我在尝试订购上面的输出时遇到了一些严重的问题。我怎样才能像现在这样按奖牌的值而不是按字母顺序排列它们?