This is my code so far, I am uncertain of how to make it so the output is in descending order or from highest to lowest. Any help would be very much appreciated.
这是我的代码到目前为止,我不知道如何使它,所以输出是降序或从最高到最低。任何帮助将非常感谢。
1 个解决方案
#1
1
You need create an List of Object array
and after use the Collection.sort
to sort the List of object array using the the count in descending order.
您需要创建一个Object数组列表,并在使用Collection.sort后使用降序计数对对象数组列表进行排序。
sample:
样品:
List<Object[]> arr = new ArrayList<Object[]>();
for (int i = 0; i < 26; i++)
{
Object obj[] = {capital[i], count[i]}; //add the capital and count to the List
arr.add(obj);
}
Collections.sort(arr, new Comparator<Object[]>() {
public int compare(Object[] c1, Object[] c2) {
return (int)c2[1] - (int)c1[1]; // will sort the count in decending order
}
});
for(int i = 0; i < arr.size(); i++)
System.out.println( "CAPITAL: " + (char)arr.get(i)[0] + " "+ "COUNT: " + (int)arr.get(i)[1]); //print all the content
}
#1
1
You need create an List of Object array
and after use the Collection.sort
to sort the List of object array using the the count in descending order.
您需要创建一个Object数组列表,并在使用Collection.sort后使用降序计数对对象数组列表进行排序。
sample:
样品:
List<Object[]> arr = new ArrayList<Object[]>();
for (int i = 0; i < 26; i++)
{
Object obj[] = {capital[i], count[i]}; //add the capital and count to the List
arr.add(obj);
}
Collections.sort(arr, new Comparator<Object[]>() {
public int compare(Object[] c1, Object[] c2) {
return (int)c2[1] - (int)c1[1]; // will sort the count in decending order
}
});
for(int i = 0; i < arr.size(); i++)
System.out.println( "CAPITAL: " + (char)arr.get(i)[0] + " "+ "COUNT: " + (int)arr.get(i)[1]); //print all the content
}