繁体   English   中英

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

[英]How do I sort an array of strings based on different parts of each string?

我必须按降序获得的金牌数量对国家/地区进行排序。 如果有多个国家并列,我需要按银牌,铜金属的数量对并列的国家进行排序,如果仍然有并列,则需要按字母顺序升序对它们进行排序。 我已经编写了一个程序,该程序比可能需要的复杂得多,但是它仍然返回无序的字符串列表。 输入是按年份给出的,第一个国家是金牌得主,第二个是银牌,第三个是铜牌。 例如:

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);
}

}

返回:

AUS 0 0 1 CHN 0 1 0 GBR 0 0 1 ITA 1 0 0 JPN 0 1 0 KOR 3 1 0 TPE 0 1 1 UKR 0 0 1

但是,在尝试订购上述输出时遇到一些严重问题。 如何像现在这样按勋章的值而不是按字母顺序对它们进行排序?

我认为解决该问题的更好方法是创建一个像这样的类:

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);
  }
}

在正确定义了compareTo (这是Comparable<T>接口的一部分 )方法之后,如果您坚持使用数组而不是列表,则应该可以使用Collections.sortArrays.sort对列表进行Arrays.sort

OlympicCountry类的toString方法负责正确格式化数据以使其显示(或您打算对其进行任何处理)。

通常,实现这样的对象来存储和处理原始数据比将所有内容都放入String或整数或某种东西中并随后尝试对其进行操作要好得多。 这就是我们毕竟可以声明我们自己的类的原因。

这是在奥林匹克国家答案中添加可比的treeSet

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);

    }
}

Java是一种面向对象的语言,请使用某些对象来执行此操作。 创建某种类别来表示一个国家/地区,并使用变量来存储金,银,铜牌以及该国家/地区的名称。

在该类中(称为Country )实现Comparable interface ,您可以在其中实现已定义的排序规则。

在运行的程序中,创建一个Country对象列表并使用Collections.sort()排序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM