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

我已经使用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());
    }`

我的意见是:

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

我的输出是:

Count: 12
Output 1(Ascending): all 1
the 1
size 1
are 1
and 1
of 1
have 1
I 1
different 1
10 1
dogs 2
Output 2(Descending): dogs 2
10 1
different 1
I 1
have 1
of 1
and 1
are 1
size 1
the 1
all 1

期望的输出:

dogs 2 \since it has more number of occurrences than any other word
10 1 \since it is a number
I 1 \since it is an uppercase letter
have 1 \followed by all the lowercase words

最佳答案 我编写了一个代码片段,以下内容适用于按升序打印输入.

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
点赞