Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何根据字符串的不同部分对字符串数组进行排序?_Java_Sorting_Map - Fatal编程技术网

Java 如何根据字符串的不同部分对字符串数组进行排序?

Java 如何根据字符串的不同部分对字符串数组进行排序?,java,sorting,map,Java,Sorting,Map,我必须按获得金牌的数量降序排列国家。如果几个国家平局,我需要按照银牌数量对平局的国家进行排序,然后是青铜金属,如果仍然平局,我需要按照字母升序对它们进行排序。我已经编写了一个比它可能需要的复杂得多的程序,但它仍然返回无序的字符串列表。输入按年份进行,第一个国家获得金牌,第二个银牌,第三个铜牌。e、 g: public class MedalTable { public static void main(String[] args){ String[] input = {"ITA JPN

我必须按获得金牌的数量降序排列国家。如果几个国家平局,我需要按照银牌数量对平局的国家进行排序,然后是青铜金属,如果仍然平局,我需要按照字母升序对它们进行排序。我已经编写了一个比它可能需要的复杂得多的程序,但它仍然返回无序的字符串列表。输入按年份进行,第一个国家获得金牌,第二个银牌,第三个铜牌。e、 g:

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);
}
公共类可插拔{
公共静态void main(字符串[]args){
字符串[]输入={“ITA JPN AUS”、“KOR TPE UKR”、“KOR KOR GBR”、“KOR CHN TPE”};
生成(输入);
}
公共静态void生成(字符串[]结果){
//你填这个代码
//“ITA JPN AUS”、“KOR TPE UKR”、“KOR KOR GBR”、“KOR CHN TPE”
String[]countrystrings=setupstuff(结果);
ArrayList goldnums=新的ArrayList();
ArrayList silvnums=新的ArrayList();
ArrayList broznums=新的ArrayList();
for(int o=0;o
}

这将返回:

澳大利亚0 0 1 中国0 1 0 GBR 0 1 ITA 100 jpn10 韩国31 0 TPE 01 UKR 0 1


然而,我在尝试订购上面的输出时遇到了一些严重的问题。我怎样才能按照奖牌的价值而不是像现在这样按字母顺序排列呢?

Java是一种面向对象的语言,使用一些对象来实现这一点。 创建一个类来代表一个国家,并使用变量来存储金牌、银牌和铜牌以及国家名称

在该类中(称之为
Country
)实现
可比接口
,您可以在其中实现已定义的排序规则


在正在运行的程序中,您创建一个
国家
对象列表,并使用
集合进行排序。排序()

我认为解决此问题的更好方法是创建如下类:

class OlympicCountry implements Comparable<OlympicCountry> {
  private int goldCount;
  private int silverCount;
  private int bronzeCount;
  private String name;

  // . . .

  public String toString() {
    return String.format("%s %d %d %d", name, goldCount, silverCount, bronzeCount);
  }

  public int compareTo(OlympicCountry other) {
    if (this.goldCount != other.goldCount)
      return this.goldCount > other.goldCount ? 1 : -1;
    if (this.silverCount != other.silverCount)
      return this.silverCount > other.silverCount ? 1 : -1;
    // . . .
    return this.name.compareTo(other.name);
  }
}
class-OlympicCountry实现可比性{
私人int goldCount;
私人帐户;
私人青铜币;
私有字符串名称;
// . . .
公共字符串toString(){
返回字符串。格式(“%s%d%d%d”,名称,goldCount,silverCount,bronzeCount);
}
公共国际比较(奥林匹克国家/地区其他){
if(this.goldCount!=其他.goldCount)
返回this.goldCount>other.goldCount?1:-1;
if(this.silverCount!=其他.silverCount)
返回this.silverCount>other.silverCount?1:-1;
// . . .
返回此.name.compareTo(其他.name);
}
}
之后
public class OlympicCountry implements Comparable<OlympicCountry> {


    public static void main(String args[]){
        Set<OlympicCountry> set = new TreeSet<OlympicCountry>(new Comparator<OlympicCountry>() {
            @Override
            public int compare(OlympicCountry o1, OlympicCountry o2) {
                return -o1.compareTo(o2);
            }
        });

        OlympicCountry c1 = new OlympicCountry();
        // set values to c1
        set.add(c1);

        OlympicCountry c2 = new OlympicCountry();
        // set values to c2
        set.add(c2);

    }
}