欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)數(shù)據(jù)按照時間排序

 更新時間:2018年09月24日 09:45:16   作者:隔壁小王66  
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)數(shù)據(jù)按照時間排序的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

經(jīng)常遇見一個列表,兩個接口的情況,兩個接口屬于兩個不同的表數(shù)據(jù),那么數(shù)據(jù)拼接回來之后,并不是按照時間排序的,看起來就相當(dāng)混亂,所以記錄一下如何對數(shù)據(jù)按照時間排序。

步驟一:

格式化日期

public static Date stringToDate(String dateString) {
    ParsePosition position = new ParsePosition(0);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date dateValue = simpleDateFormat.parse(dateString, position);
    return dateValue;
  }

步驟二:

對拼接的列表進(jìn)行排序

private void sortData(ArrayList<CourseModel> mList) {
    Collections.sort(mList, new Comparator<CourseModel>() {
      /**
       *
       * @param lhs
       * @param rhs
       * @return an integer < 0 if lhs is less than rhs, 0 if they are
       *     equal, and > 0 if lhs is greater than rhs,比較數(shù)據(jù)大小時,這里比的是時間
       */
      @Override
      public int compare(CourseModel lhs, CourseModel rhs) {
        Date date1 = DateUtil.stringToDate(lhs.getCREATE_TIME());
        Date date2 = DateUtil.stringToDate(rhs.getCREATE_TIME());
        // 對日期字段進(jìn)行升序,如果欲降序可采用after方法
        if (date1.before(date2)) {
          return 1;
        }
        return -1;
      }
    });
    adapter.replaceAll(mList);
  }

直接調(diào)用這個方法,數(shù)據(jù)類型改造一下即可。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論