arraylist排序
先说下原因,最近项目中出现了获取网络数据混乱的情况,经过仔细查看才知道是加入集合的顺序出了问题,由于我是循环获取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; private String 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);//只需要这一句就可以了
相关阅读
sort函数可以排序任何类型的函数,包括自己写的结构体 (sort函数效率高) 头文件#include<algorithm> 排序数值从小到大int num[10
概述 手写排序算法几乎是程序员面试必问的题目,大多数人都会选择写冒泡排序,如果此时你写的是其他改进过的排序算法,相信会让面试官
排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记
每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。 选择排序是
DNF4月4日的特殊图腾是什么?DNF4月4日特殊图腾排序怎么组合?很多小伙伴不知道这本周的特殊图腾怎么排,下面小编为大家带来了DNF4月