Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 列表的排序<;对象>;_Android_Sorting_Listobject - Fatal编程技术网

Android 列表的排序<;对象>;

Android 列表的排序<;对象>;,android,sorting,listobject,Android,Sorting,Listobject,我需要将产品按价格从高到低&从低到高进行分类。我使用以下代码从低到高进行了分类。但不知道如何实现从高到低 你的回答更令人感激 public static Comparator<Restaurant_Beam> strPriceFilterL2H = new Comparator<Restaurant_Beam>() { @Override public int compare(Restaurant_Beam lhs, Restaurant_

我需要将产品按价格从高到低&从低到高进行分类。我使用以下代码从低到高进行了分类。但不知道如何实现从高到低

你的回答更令人感激

 public static Comparator<Restaurant_Beam> strPriceFilterL2H = new Comparator<Restaurant_Beam>() {

      @Override
        public int compare(Restaurant_Beam lhs, Restaurant_Beam rhs) {
            int CompareResult = 0;
            if (Config.L2HFilterClicked == "L2H") {
                CompareResult = (int) Math.round(Double.parseDouble(lhs.getIG_SALES_PRICE()) - Double.parseDouble(rhs.getIG_SALES_PRICE()));

            } 
//Used else if for H2L.But did not get it as perfect

else if (Config.L2HFilterClicked == "H2L") {
                CompareResult = (int) Math.round(Double.parseDouble(lhs.getIG_SALES_PRICE()) + Double.parseDouble(rhs.getIG_SALES_PRICE()));

            }
            return CompareResult;
        }
    };
公共静态比较器strpricefilter2h=新比较器(){
@凌驾
公共内部比较(餐厅左侧、餐厅右侧){
int CompareResult=0;
如果(Config.L2HFilterClicked==“L2H”){
CompareResult=(int)Math.round(Double.parseDouble(lhs.getIG_SALES_PRICE())-Double.parseDouble(rhs.getIG_SALES_PRICE());
} 
//如果用于H2L,则使用else。但没有得到完美的效果
else if(Config.L2HFilterClicked==“H2L”){
CompareResult=(int)Math.round(Double.parseDouble(lhs.getIG_SALES_PRICE())+Double.parseDouble(rhs.getIG_SALES_PRICE());
}
返回比较结果;
}
};

将第二个比较表达式更改为此表达式:

CompareResult = (int) Math.round(Double.parseDouble(Double.parseDouble(rhs.getIG_SALES_PRICE()) - Double.parseDouble(lhs.getIG_SALES_PRICE()));
我还想指出一些事情

  • 考虑与epsilon进行
    Double
    比较
  • 在比较对象之前检查对象是一种很好的做法
  • 每次比较时解析都是非常糟糕的设计。如果你事先在某个地方解析值,最好考虑改变你的类型。目前效率不高

将第二个比较表达式更改为此表达式:

CompareResult = (int) Math.round(Double.parseDouble(Double.parseDouble(rhs.getIG_SALES_PRICE()) - Double.parseDouble(lhs.getIG_SALES_PRICE()));
我还想指出一些事情

  • 考虑与epsilon进行
    Double
    比较
  • 在比较对象之前检查对象是一种很好的做法
  • 每次比较时解析都是非常糟糕的设计。如果你事先在某个地方解析值,最好考虑改变你的类型。目前效率不高

非常感谢@Ivan Leonenko非常感谢@Ivan Leonenko