android 根据实体里的日期进行排序

// 根据id查找 并排序(降序)
    @SuppressLint("SimpleDateFormat")
    public List<Payout> getPayoutListByBookId(int bookId) {
        // TODO Auto-generated method stub
        List<Payout> listfrist = payoutDAO
                .getPayout(" and state = 1 and bookId = " + bookId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //做一个冒泡排序,大的在数组的前列  
        for (int i = 0; i < listfrist.size(); i++) {
            for (int j = i+1; j < listfrist.size(); j++) {
                ParsePosition pos1=new ParsePosition(0);
                ParsePosition pos2=new ParsePosition(0);
                // 日期转换string
                String str1=DateString.ConverToString(listfrist.get(i).getPayoutDate());
                String str2=DateString.ConverToString(listfrist.get(j).getPayoutDate());
                
                Date date1=sdf.parse(str1,pos1);
                Date date2=sdf.parse(str2,pos2);
                Log.e("", "><><"+str1+"------------"+str2);
                if (date1.before(date2)) {//如果队前日期靠前,调换顺序
                    Payout temp=listfrist.get(i);
                    listfrist.set(i, listfrist.get(j));
                    listfrist.set(j, temp);
                }
            }
        }        
        return listfrist;
    }

posted on 2016-08-28 13:49  baoshijie  阅读(390)  评论(0)    收藏  举报

导航