如何根据POJO的特定字段值对集合进行排序?

时间:2014-12-25 08:52:16

标签: java sorting collections pojo

我有一个下面提到的课程(POJO),

public class DummyPage{

   public DummyPage(int id, String month){
     this.id = id;
     this.month = month;
   }

   public DummyPage(){
   }

   private int id
   private String  month;

   //getter and setters
}

还有一段代码

public static void main(String[] args){

   List<DummyPage> list = new ArrayList<DummyPage>();
   list.add(new DummyPage(1,"March"));
   list.add(new DummyPage(2,"September"));
   list.add(new DummyPage(3,"December"));
   list.add(new DummyPage(4,"May"));
   list.add(new DummyPage(5,"April"));
   list.add(new DummyPage(6,"January"));
   list.add(new DummyPage(7,"August"));
   list.add(new DummyPage(8,"March"));
   list.add(new DummyPage(9,"December"));
   list.add(new DummyPage(10,"October"));
   list.add(new DummyPage(11,"July"));
   list.add(new DummyPage(12,"November"));
   list.add(new DummyPage(13,"February"));
   list.add(new DummyPage(14,"May"));
   list.add(new DummyPage(15,"June"));      

}

我的实际POJO包含更多字段,实际上list包含更多对象。

预期结果:
我需要根据月份名称对声明的list进行排序。
具有January月份名称的所有对象必须在输出列表中显示first 具有February月份名称的所有对象必须在输出列表中显示second 等所有月份。

哪种方式以给定的方式对这些类型的对象进行排序?我可以使用Comparator吗?如果是,我该如何实施呢?提前谢谢。

3 个答案:

答案 0 :(得分:3)

您可以定义自定义比较器并将其传递给Collections.sort(),如@Robby所示,您可以使用月份枚举

public class CustomComparator implements Comparator<DummyPage> {
    @Override
    public int compare(DummyPage o1, DummyPage o2) {
        return Month.valueOf(o1.getMonth().toUpperCase()).compare(Month.valueOf(o2.getMonth().toUpperCase()));
    }
}

Collections.sort(list , new CustomComparator());

同样看起来您已在DummyPage.id字段中有月号,它可以直接用于比较器的compare方法。或者,如果您正在寻找如何将月份名称转换为数字,则可以使用SimpleDateFormat

public int convertMonthToNumber(String month){
     Date date = new SimpleDateFormat("MMMM", Locale.ENGLISH).parse(month);
     Calendar cal = Calendar.getInstance();
     cal.setTime(date);
     return cal.get(Calendar.MONTH);
}

答案 1 :(得分:0)

您可以将月份字符串转换为日历月份,然后您可以将它们进行比较。

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CustomComparator implements Comparator<DummyPage> {
    @Override
    public int compare(DummyPage object1, DummyPage object2) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new SimpleDateFormat("MMM").parse(object1.getMonth()));
        int object1Month = cal.get(Calendar.MONTH) + 1;
        cal.setTime(new SimpleDateFormat("MMM").parse(object2.getMonth()));
        int object2Month = cal.get(Calendar.MONTH) + 1;
        return object1Month - object2Month;
    }
}

Collections.sort(list , new CustomComparator());

答案 2 :(得分:-1)

...
List< DummyPage > sortedList = new ArrayList< DummyPage >(list);
Collections.sort(sortedList, new Comparator<DummyPage>() {
        public int compare(DummyPage p1, DummyPage p2) {
           return  Month.valueOf(p1.getMonth()) .compareTo(Month.valueOf(p2.getMonth());
           //Wrong : return p1.getMonth().compareTo(p2.getMonth());
        }
});

// Now the sortedList is sorted Based on the month