按 Java 中的月份名称对列表<Object> 排序

Sort List<Object> by month name in Java

我在 Java 的 ArrayList 中有以下数据。我正在尝试按一年中的月份对它进行排序。它应该按 12 月、11 月等排序。我尝试了不同的技术,但其中 none 有效。

month featureName featureCount appName
April login 1 Mobile Banking
April login 1 Retail Bank Portal
April S3 1 Mobile Banking
April update profile 1 Markets Research Portal
April update profile 1 Retail Bank Portal
December login 1 Retail Bank Portal
December Replace PAN Card 1 Retail Bank Portal
December update profile 1 Retail Bank Portal
December update user profile api 1 Retail Bank Portal
January FI unfreeze PAN Card 1 Retail Bank Portal
January login 1 Retail Bank Portal
January Replace PAN Card 1 Retail Bank Portal
March login 1 Retail Bank Portal
March update profile 1 Retail Bank Portal
November login 1 Retail Bank Portal
November Replace PAN Card 1 Retail Bank Portal
October Replace PAN Card 1 Retail Bank Portal
October S3 1 Retail Bank Portal
October View Account Summary 1 Retail Bank Portal

这是我的代码:

public class FeatureAnalyzeDTO {
private String featureName;
private String month;
private int featureCount;
private String appName;

private int featureCountForAMonth;
private double percentage;
private int cumulativeSum;
}


List<FeatureAnalyzeDTO> featureAnalyzeDTOList = getFeatureAnalyzeDTOS(featureAnalyzeInterfaceList);

我试过使用比较器,但没有用。我怎样才能按月份名称排序?

如果您使用 java8

,请尝试以下代码
 List<FeatureAnalyzeDTO> featureAnalyzeInterfaceListUpdated= featureAnalyzeInterfaceList.stream().sorted(
                Comparator.comparingInt(o -> Month.valueOf(o.month.toUpperCase()).getValue())).collect(Collectors.toList());

首先,创建一个包含 January 到 December 的 monthList。

List<String> monthList = Arrays.asList("January", "February", "March", "April",
    "May", "June", "July", "August", "September",
    "October", "November", "December");

然后,使用 List.sort 并实现 Comparator,按降序比较月份。

featureAnalyzeInterfaceList.sort((dto1, dto2) -> {
    if (monthList.indexOf(dto1.month) > monthList.indexOf(dto2.month)) {
        return -1;
    } else if (monthList.indexOf(dto1.month) < monthList.indexOf(dto2.month)) {
        return 1;
    }
    return 0;
});

如果您至少使用 Java 8,那么您应该使用 java.time.Month   enum。我在下面的代码中使用它。

如果您使用 [至少] Java 14,那么您可以使用 record 而不是 class。参考JEP 395. In the below code, FeatureAnalyzeDTO is a record and not a class. I assume that the DTO in the [class] name means Data Transfer Object which implies to me that it may need to be serializable. Note that a java record can also be serializable. Refer to the article from Chris Hegarty entitled Serializable Records. Using a record simply saves you from writing the usual methods including getters, equals and toString. Note, however, that records are [meant to be] immutable.

在下面的代码中,我创建了 ListFeatureAnalyzeDTO 个实例以及一个 Comparator for sorting the list by month, descending, i.e. FeatureAnalyzeDTO instances whose month is December are first in the list while those whose month is January are last. The Comparator is implemented via a lambda expression。在 Java 8.

中添加了 Lambda 表达式
import java.io.Serializable;
import java.time.Month;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public record FeatureAnalyzeDTO(String featureName,
                                String month,
                                int featureCount,
                                String appName) implements Serializable {

    public static void main(String[] args) {
        List<FeatureAnalyzeDTO> list = new ArrayList<>(List.of(new FeatureAnalyzeDTO("login", "April", 1, "Mobile Banking"),
                                                               new FeatureAnalyzeDTO("login", "April", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("S3", "April", 1, "Mobile Banking"),
                                                               new FeatureAnalyzeDTO("update profile", "April", 1, "Markets Research Portal"),
                                                               new FeatureAnalyzeDTO("update profile", "April", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("login", "December", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("Replace PAN Card", "December", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("update profile", "December", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("update user profile api", "December", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("FI unfreeze PAN Card", "January", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("login", "January", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("Replace PAN Card", "January", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("login", "March", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("update profile", "March", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("login", "November", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("Replace PAN Card", "November", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("Replace PAN Card", "October", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("S3", "October", 1, "Retail Bank Portal"),
                                                               new FeatureAnalyzeDTO("View Account Summary", "October", 1, "Retail Bank Portal")));
        Comparator<FeatureAnalyzeDTO> comp = (o1, o2) -> Month.valueOf(o2.month().toUpperCase()).getValue() - Month.valueOf(o1.month().toUpperCase()).getValue();
        list.sort(comp);
        list.forEach(System.out::println);
    }
}

运行 上述代码产生以下结果:

FeatureAnalyzeDTO[featureName=login, month=December, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=Replace PAN Card, month=December, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=update profile, month=December, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=update user profile api, month=December, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=login, month=November, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=Replace PAN Card, month=November, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=Replace PAN Card, month=October, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=S3, month=October, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=View Account Summary, month=October, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=login, month=April, featureCount=1, appName=Mobile Banking]
FeatureAnalyzeDTO[featureName=login, month=April, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=S3, month=April, featureCount=1, appName=Mobile Banking]
FeatureAnalyzeDTO[featureName=update profile, month=April, featureCount=1, appName=Markets Research Portal]
FeatureAnalyzeDTO[featureName=update profile, month=April, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=login, month=March, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=update profile, month=March, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=FI unfreeze PAN Card, month=January, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=login, month=January, featureCount=1, appName=Retail Bank Portal]
FeatureAnalyzeDTO[featureName=Replace PAN Card, month=January, featureCount=1, appName=Retail Bank Portal]