/**
* 商品排序
* @param list 商品列表
* @param type 排序类型
* @param desc 价格排序(true为正序,false倒序)
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.N)
public static List<ShopItemsBean> getshopsort(List<ShopItemsBean> list, String type, boolean desc) {
list.sort(new Comparator<ShopItemsBean>() {
@Override
public int compare(ShopItemsBean shopItemsBean, ShopItemsBean t1) {
if (type.equals("sales")) {//根据销量排序(销量高的在前面)
return t1.getSales() - shopItemsBean.getSales();
}
if (type.equals("price")) {//根据价格排序(desc为true则按价格正序排列,false倒序)
if (desc) {
return t1.getPrice() - shopItemsBean.getPrice();
} else {
return shopItemsBean.getPrice() - t1.getPrice();
}
}
if (type.equals("comments")) {//根据评论数排序(评论高的在前面)
return t1.getComment_count() - shopItemsBean.getComment_count();
}//根据最新排序,id数最高为最新数据
return t1.getId() - shopItemsBean.getId();
}
});
return list;
}
Android 根据list列表数据按照类型排序
最新推荐文章于 2021-12-02 19:59:33 发布