当前位置:   article > 正文

对ArrayList中按照T的某个属性进行排序_android 快速实现arraylist根据某个属性排序

android 快速实现arraylist根据某个属性排序

对ArrayList中按照T的某个属性进行排序

1、先定义一个比较的方法

    public class A implements Comparator<Object> {
        public int compare(Object o1, Object o2) {
            C c1=(C)o1;
            C c2=(C)o2;
            int flag=c1.getStuDate().compareTo(c2.getStuDate());
            Log.i("MainActivity","----"+flag);
            return flag;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2、书写数据源ArrayList中实体类T

package com.xht.android.sortstime;

/**
 * Created by Administrator on 2016-10-17.
 */

public class C {
        private String stuDate;
        private String stuName;
        private String stuSex;

        /**
         * @return the stuName
         */
        public String getStuName() {
            return stuName;
        }
        /**
         * @return the stuDate
         */
        public String getStuDate() {
            return stuDate;
        }
        /**
         * @param stuDate the stuDate to set
         */
        public void setStuDate(String stuDate) {
            this.stuDate = stuDate;
        }
        /**
         * @param stuName the stuName to set
         */
        public void setStuName(String stuName) {
            this.stuName = stuName;
        }
        /**
         * @return the stuSex
         */
        public String getStuSex() {
            return stuSex;
        }
        /**
         * @param stuSex the stuSex to set
         */
        public void setStuSex(String stuSex) {
            this.stuSex = stuSex;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

3、主活动中获取数据源ArrayList的数据,打印出原集合中的数据

 //原集合中的数据
    for(int i=0;i<list.size();i++){
        C c=list.get(i);
        Log.i(TAG, "--------;"+c.getStuDate()+" "+c.getStuName()+"  "+c.getStuSex());
    }
  • 1
  • 2
  • 3
  • 4
  • 5

4、按照时间先后顺序对集合排序,并输出

  //按时间先后排序后的数据   日期:前--->>后
    Log.i(TAG, "--------;-----------");
    A cm = new A();
    Collections.sort(list,cm);
    for(int i=0;i<list.size();i++){
        C c=list.get(i);
        Log.i(TAG, "--------;"+c.getStuDate()+" "+c.getStuName()+"  "+c.getStuSex());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/715581
推荐阅读
相关标签
  

闽ICP备14008679号