對ArrayList集合裡面資料排序
阿新 • • 發佈:2019-01-06
先說下原因,最近專案中出現了獲取網路資料混亂的情況,經過仔細檢視才知道是加入集合的順序出了問題,由於我是迴圈獲取id,然後再迴圈請求資料,請求成功之後就把實體類中的資料加入到ArrayList集合中,可是我忽略了網路請求的耗時不確定,可能第二次請求的耗時小於第一次請求,這就導致加入集合的順序不能保證,所以出現了錯亂,所以,我決定在加入集合後給他們排序。
經過查閱資料得知,解決的辦法有兩個,一個是通過Comparator,一個是實現Comparable介面,我在這裡選擇了第二種,使所在的類實現Comparable介面,重寫compareTo,和toString兩個方法。
程式碼部分 :
/*** Created by zhangjiaqi on 16/11/5. */ public class GoodsForTypeEntity implements Comparable<GoodsForTypeEntity> { public GoodsForTypeEntity(int id, String name) { this.id = id; this.name = name; } private int id; private String name; public int count; privateString dishes_id; private String mct_id; private String dishes_cate_id; private String dishware_id; private String dishes_name; private String dishes_desc; private String dishes_pic; private String dishes_pic_list; private String dishes_price; private String deposit_price; private String sales_volume; private String is_selling; private String dishes_stock; private String ctime; private String utime; private String status; private String is_hot; private String remark; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getIs_hot() { return is_hot; } public void setIs_hot(String is_hot) { this.is_hot = is_hot; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getCtime() { return ctime; } public void setCtime(String ctime) { this.ctime = ctime; } public String getUtime() { return utime; } public void setUtime(String utime) { this.utime = utime; } public String getDishes_stock() { return dishes_stock; } public void setDishes_stock(String dishes_stock) { this.dishes_stock = dishes_stock; } public String getIs_selling() { return is_selling; } public void setIs_selling(String is_selling) { this.is_selling = is_selling; } public String getDeposit_price() { return deposit_price; } public void setDeposit_price(String deposit_price) { this.deposit_price = deposit_price; } public String getSales_volume() { return sales_volume; } public void setSales_volume(String sales_volume) { this.sales_volume = sales_volume; } public String getDishes_price() { return dishes_price; } public void setDishes_price(String dishes_price) { this.dishes_price = dishes_price; } public String getDishes_pic_list() { return dishes_pic_list; } public void setDishes_pic_list(String dishes_pic_list) { this.dishes_pic_list = dishes_pic_list; } public String getDishes_pic() { return dishes_pic; } public void setDishes_pic(String dishes_pic) { this.dishes_pic = dishes_pic; } public String getDishes_desc() { return dishes_desc; } public void setDishes_desc(String dishes_desc) { this.dishes_desc = dishes_desc; } public String getDishes_name() { return dishes_name; } public void setDishes_name(String dishes_name) { this.dishes_name = dishes_name; } public String getDishware_id() { return dishware_id; } public void setDishware_id(String dishware_id) { this.dishware_id = dishware_id; } public String getDishes_cate_id() { return dishes_cate_id; } public void setDishes_cate_id(String dishes_cate_id) { this.dishes_cate_id = dishes_cate_id; } public String getMct_id() { return mct_id; } public void setMct_id(String mct_id) { this.mct_id = mct_id; } public String getDishes_id() { return dishes_id; } public void setDishes_id(String dishes_id) { this.dishes_id = dishes_id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int compareTo(GoodsForTypeEntity goodsForTypeEntity) {//使其按照從小到大的順序排列 return id - goodsForTypeEntity.id; } @Override public String toString() {//在輸入物件時按照以方式象輸出 return "GoodsForTypeEntity[id:" + id + "]"; }
}
呼叫非常簡單
for (int i = 0; i < entity.getData().getDishes().size(); i++) { GoodsForTypeEntity goodsForTypeEntity = new GoodsForTypeEntity(typeId, typeName); // goodsForTypeEntity.setDishes_cate_id("" + dishes_cate_id); setEntityParam(goodsForTypeEntity, entity.getData().getDishes().get(i)); goodsList.add(goodsForTypeEntity); } } Collections.sort(goodsList);//只需要這一句就可以了