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

标签 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());
    }`

我的输入是:

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

关于java - 如何根据 ascii 值对 ArrayList 的元素进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39324072/

相关文章:

java.lang.String 无法转换为 java.util.List

java - 自定义 maven 插件 : install and execute the plugin in one build

bash - 如何打印文件中的最高/最长值

algorithm - Jira 的新故事 Lexorank 算法

arrays - 根据 SWIFT 中的字符串数组对自定义数组进行排序

java - junit/mockito 中的断言不正确

Java GC(分配失败)

java - Openshift jobssas 7 云正在制造 war ,但没有部署它

javascript - 实现在 Angular2 中对对象数组进行排序

arrays - Mongoose如何对查询返回的对象数组进行排序