如何根据ascii值对ArrayList的元素进行排序?

时间:2016-09-05 04:45:51

标签: java sorting

我已经使用Scanner读取了一个文件,然后使用HashMap和ArrayList根据单词的出现次数(升序和降序)对单词进行排序,一切正常。但是我希望输出的排序方式是首先显示数字然后是大写,然后是小写。

以下是我的相同代码:

`Scanner scanner = new Scanner(new File("file"));
    Map<String, Integer> map = new HashMap<String, Integer>();
    int count=0;
    String whole="";
    while (scanner.hasNext())
        {
        count++;
        String word = scanner.next();
        whole=whole + " " + word;
        if (map.containsKey(word))
            {
            map.put(word, map.get(word)+1);
            }
        else
            {
            map.put(word, 1);
            }
        }

    List<Map.Entry<String, Integer>> entries = new ArrayList<Entry<String,Integer>>( map.entrySet());

    Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {

        @Override
        public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
            return a.getValue().compareTo(b.getValue());
        }
    });

    System.out.println("Count: " +count);
    System.out.print("Output 1(Ascending): ");
    for(int j = 0; j < map.size(); j++){
        System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
    }

    System.out.print("Output 2(Descending): ");
    for(int i = 0; i < map.size(); i++){
        System.out.println(entries.get(entries.size() - i - 1).getKey()+" "+entries.get(entries.size() - i - 1).getValue());
    }`

我的意见是:

  

我有10只狗,所有的狗都有不同的大小

我的输出是:

  

数:12
  输出1(升序):全1   1   尺寸1
  是1
  和1
  1   有1
  我1   不同1
  10 1
  狗2   输出2(降序):狗2   10 1
  不同1
  我1   有1
  1   和1
  是1
  尺寸1
  1   全部1

期望的输出:

  

狗2 \因为它出现的次数比任何其他单词多   10 1 \因为它是一个数字
  我1 \因为它是一个大写字母
  有1 \后跟所有小写单词

3 个答案:

答案 0 :(得分:2)

我已经编写了一段代码片段,以下内容可用于按升序打印输入。

String input = "I have 10 dogs and all the dogs are of different size";
String [] inputSplit = input.split(" ");

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

for (int i=0; i < inputSplit.length; i++) {
    String word = inputSplit[i];
    if (map.containsKey(word)) {
        map.put(word,  map.get(word) + 1);
    }
    else {
        map.put(word, 1);
    }
}

List<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
    @Override
    public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
        int compareWordCount = a.getValue().compareTo(b.getValue());

        if (compareWordCount == 0) {
            return a.getKey().compareTo(b.getKey());
        }
        return compareWordCount;
    }
});

for (int j=0; j < entries.size(); j++) {
    System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
}

结果

10 1
I 1
all 1
and 1
are 1
different 1
have 1
of 1
size 1
the 1
dogs 2

使用降序排序

String input = "I have 10 dogs and all the dogs are of different size";

String [] inputSplit = input.split(" ");

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

for(int i = 0; i < inputSplit.length; i++){
    String word = inputSplit[i];
    if(map.containsKey(word)){
        map.put(word,  map.get(word) + 1);
    }
    else{
        map.put(word, 1);
    }
}

List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());

Comparator <Entry<String, Integer>> ascComparator = new Comparator<Entry<String, Integer>>(){

    @Override
    public int compare(Entry<String, Integer> a, Entry<String, Integer> b) {

        int compareWordCount = a.getValue().compareTo(b.getValue());

        if(compareWordCount == 0){
            return a.getKey().compareTo(b.getKey());
        }
        return compareWordCount;
    }

};

Comparator <Entry<String, Integer>> descComparator = new Comparator<Entry<String, Integer>>(){

    @Override
    public int compare(Entry<String, Integer> a, Entry<String, Integer> b) {

        int compareWordCount = a.getValue().compareTo(b.getValue());

        if(compareWordCount == 0){
            return b.getKey().compareTo(a.getKey());
        }
        return compareWordCount;
    }

};

System.out.println("Ascending Sort");
Collections.sort(entries, ascComparator);       
for(int j = 0; j < entries.size(); j++){
    System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
}

System.out.println("\nDescending Sort");
Collections.sort(entries, descComparator);

for(int j = 0; j < entries.size(); j++){
    System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
}

结果

Ascending Sort
10 1
I 1
all 1
and 1
are 1
different 1
have 1
of 1
size 1
the 1
dogs 2

Descending Sort
the 1
size 1
of 1
have 1
different 1
are 1
and 1
all 1
I 1
10 1
dogs 2

答案 1 :(得分:0)

您可以扩展比较逻辑以添加ascii排序逻辑

        public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
            int i = a.getValue().compareTo(b.getValue());
            if(i==0) {
                i = b.getKey().compareTo(a.getKey());
            }
            return i;
        }

希望这有帮助。

答案 2 :(得分:0)

只需更新比较方法部分:

    Scanner scanner = new Scanner(new File("file"));
    Map<String, Integer> map = new HashMap<String, Integer>();
    int count=0;
    String whole="";
    while (scanner.hasNext())
        {
        count++;
        String word = scanner.next();
        whole=whole + " " + word;
        if (map.containsKey(word))
            {
            map.put(word, map.get(word)+1);
            }
        else
            {
            map.put(word, 1);
            }
        }

    List<Map.Entry<String, Integer>> entries = new ArrayList<Entry<String,Integer>>( map.entrySet());

    Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {

        @Override
        public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
            int compareResult = a.getValue().compareTo(b.getValue());
            if (compareResult != 0) {
                return compareResult;
            } else {
                //Here, when the occurance numbers are equal, then compare
                //with the key values where the texts are stored
                return a.getKey().compareTo(b.getKey());
            }
        }
    });

    System.out.println("Count: " +count);
    System.out.print("Output 1(Ascending): ");
    for(int j = 0; j < map.size(); j++){
        System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
    }

    System.out.print("Output 2(Descending): ");
    for(int i = 0; i < map.size(); i++){
        System.out.println(entries.get(entries.size() - i - 1).getKey()+" "+entries.get(entries.size() - i - 1).getValue());
    }