Home > OS >  Sort List<Object> by month name in Java
Sort List<Object> by month name in Java

Time:04-29

I am having following data in ArrayList in Java. I am trying to sort it by month in a year. It should sort by December, November, and so on. I have tried different techniques but none of those is working.

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

Here is my Code:

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

I have tried using a Comparator but that did not work. How could I sort it by month name?

CodePudding user response:

First, create a monthList which contains January to December .

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

Then, use List.sort and implement Comparator which compare month by descending order.

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

CodePudding user response:

Try below code if you are using java8

 List<FeatureAnalyzeDTO> featureAnalyzeInterfaceListUpdated= featureAnalyzeInterfaceList.stream().sorted(
                Comparator.comparingInt(o -> Month.valueOf(o.month.toUpperCase()).getValue())).collect(Collectors.toList());

CodePudding user response:

If you are using at least Java 8, then you should use the java.time.Month   enum. I use it in the below code.

If you are using [at least] Java 14, then you can use a record rather than a class. Refer to 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.

In the below code, I create a List of FeatureAnalyzeDTO instances as well as a 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. Lambda expressions were added in Java 8.

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

Running the above code produces the following:

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]
  • Related