Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何根据POJO的特定字段值对集合进行排序?_Java_Sorting_Collections_Pojo - Fatal编程技术网

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

Java 如何根据POJO的特定字段值对集合进行排序?,java,sorting,collections,pojo,Java,Sorting,Collections,Pojo,我有下面提到的一门课(POJO) 还有一段代码 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

我有下面提到的一门课(
POJO

还有一段代码

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"));      

}
publicstaticvoidmain(字符串[]args){
列表=新的ArrayList();
增加(新的DummyPage(1,“3月”);
增加(新的DummyPage(9月2日);
新增(第3页,12月);
新增(第4页,“5月”);
增加(新的DummyPage(4月5日);
增加(新的DummyPage(6,“1月”);
增加(新的DummyPage(8月7日);
增加(新的DummyPage(3月8日);
新增(第9页,“12月”);
增加(新的DummyPage(10月10日);
增加(新的DummyPage(7月11日);
增加(新的DummyPage(11月12日);
增加(新的DummyPage(2月13日);
增加(新的DummyPage(5月14日);
增加(新的DummyPage(6月15日);
}
我的实际
POJO
有更多的字段,而且实际上
list
有更多的对象

预期结果:
我需要根据月份名称对声明的
列表进行排序
在输出列表中,具有
janur
月名的所有对象必须位于
的第一位。
在输出列表中,具有
二月
month name的所有对象必须位于
second

等整整几个月


以给定方式对这些类型的对象进行排序的最佳方法是什么?我可以使用比较器吗?如果是,我如何实施?提前感谢。

您可以定义一个自定义比较器并将其传递给
Collections.sort()
,正如所建议的,您可以像

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()));
    }
}
看起来您在
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);
}
。。。
列表sortedList=new ArrayList(列表);
Collections.sort(sortedList,new Comparator(){
公共整数比较(DummyPage p1,DummyPage p2){
返回Month.valueOf(p1.getMonth()).compareTo(Month.valueOf(p2.getMonth());
//错误:返回p1.getMonth().compareTo(p2.getMonth());
}
});
//现在,sortedList根据月份进行排序

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

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());
import java.text.simpleDataFormat;
导入java.util.Calendar;
公共类CustomComparator实现了Comparator{
@凌驾
公共int比较(DummyPage对象1,DummyPage对象2){
Calendar cal=Calendar.getInstance();
setTime(新的SimpleDataFormat(“MMM”).parse(object1.getMonth());
int object1mount=cal.get(Calendar.MONTH)+1;
setTime(新的SimpleDataFormat(“MMM”).parse(object2.getMonth());
int object2mmonth=cal.get(Calendar.MONTH)+1;
返回对象1个月-对象2个月;
}
}
Collections.sort(list,new CustomComparator());

到目前为止您尝试了什么?只需一个提示,您可以将自定义比较器传递给集合。排序方法编写一个由枚举支持的
比较器。@Hi@Karna,谢谢,但我知道比较器。。我也在谷歌上看到了很多关于比较器的示例。但是找不到使用它的方法。@Robby Cornelissen,谢谢,但我没有看到它。@Robby Cornelissenot获取您想要的信息。.有代码快照或引用链接的示例吗?是的,这只是个问题。比较月份的逻辑是什么?我可以轻松比较
id
。@Karna如果使用枚举:
Month.valueOf(o1.getMonth().toUpperCase())可能需要大写月份
另外,出于性能原因,我建议不要使用
SimpleDateFormat
Calendar
@RobbyCornelissen-Yup解决方案,Java 8中也引入了月枚举,因此如果OP更早,那么
SimpleDateFormat
将是一个不错的选择version@Karna若OP使用的是早期版本,那个么impl就很简单了一个月枚举:
public enum month{1月、2月、3月、4月、5月、6月、7月、8月、9月、10月、11月、12月}
See,它适合于一个注释;-)它将是一个字符串比较,而不是月份比较。它将给出
APRIL
第一个月,而不是
APRIL
。哎呀,我不好。需要是Month.valueOf(p1.getMonth()).compareTo(Month.valueOf(p2.getMonth())。我已经编辑了比较方法op在
Dummypage.id
字段中已经有了月号,可以使用它来代替从月名派生相同的月号
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);
}
...
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
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());