根据数值以降序排列番石榴表 [英] Sorting Guava tables in descending order based on values

查看:119
本文介绍了根据数值以降序排列番石榴表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用番石榴表格来存储表格格式的值。我想知道一些函数,它根据表中的值执行降序排序......您可以对此进行一些观点。谢谢。

解决方案

与地图一样,您应该复制 cellSet(),按值排序单元格,然后把它放入一个保存顺序的 ImmutableTable

  Ordering< Table.Cell< String,String,Integer>>比较器= 
新订单< Table.Cell< String,String,Integer>>(){
public int compare(
Table.Cell< String,String,Integer> cell1,
Table.Cell< String,String,Integer> cell2){
return cell1.getValue()。compareTo(cell2.getValue());
}
};
//以递增值的顺序对单元格进行排序,但我们希望递减顺序...
ImmutableTable.Builder< String,String,Integer>
sortedBuilder = ImmutableTable.builder(); //保留插入顺序
for(Table.Cell< String,String,Integer> cell:
comparator.reverse()。sortedCopy(table.cellSet())){
sortedBuilder.put (细胞);
}
return sortedBuilder.build();

无论如何,这或多或少都是您编写的用于按值排序地图的代码。

I am planning to use guava tables for storing my values in the table format. I would like to know some function which performs the descending order sorting based on the value in the table ... Could you people throw some views about this. Thank you.

解决方案

Just like with maps, you should copy the cellSet(), sort the cells by the values, and then put that into an order-preserving ImmutableTable.

Ordering<Table.Cell<String, String, Integer>> comparator =
  new Ordering<Table.Cell<String, String, Integer>>() {
    public int compare(
        Table.Cell<String, String, Integer> cell1, 
        Table.Cell<String, String, Integer> cell2) {
      return cell1.getValue().compareTo(cell2.getValue());
  }
};
// That orders cells in increasing order of value, but we want decreasing order...
ImmutableTable.Builder<String, String, Integer>
    sortedBuilder = ImmutableTable.builder(); // preserves insertion order
for (Table.Cell<String, String, Integer> cell :
    comparator.reverse().sortedCopy(table.cellSet())) {
  sortedBuilder.put(cell);
}
return sortedBuilder.build();

This is more or less exactly the code you'd write to sort a map by values, anyway.

这篇关于根据数值以降序排列番石榴表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆