List collection sorting: the product list is sorted according to sales or price

We generally use this knowledge on the product search page of the mall

 

That's it, and here's our sorting code:

          //排序
        Collections.sort(list, new Comparator<KindsGoodsListModel.MessageBean.GoodsBean>() {
            @Override
            public int compare(KindsGoodsListModel.MessageBean.GoodsBean goodsBean, KindsGoodsListModel.MessageBean.GoodsBean t1) {
                //排序的要求(根据价格还是销量:价格)
                int i = goodsBean.getGoods_price() - t1.getGoods_price();
                if (i == 0) {
                    return goodsBean.getGoods_price() - t1.getGoods_price();
                }
                return i;
            }
        });
        //刷新适配器
        kindsGoodsListAdapter.notifyDataSetChanged();

Use the method that comes with the list to achieve sorting. The default sorting starts from the lowest price. If there is a ranking from high price to low price, use this method

Collections.reverse();

The passed value is a set whose order needs to be reversed, and then refresh the adapter.

Guess you like

Origin blog.csdn.net/m0_46366678/article/details/123352637