简体   繁体   中英

How to sort a list of months with years

I have a list of months with years such as: [12-2014,11-2012,5-2014,8-2012] and I have to sort them with the most recent on top (or the latest date on top) eg. [12-2014,5-2014,11-2012,8-2012] .
Does anybody have any idea on how to do this in Java efficiently?

EDIT:
The class YearMonth is not available, I'm on Java 7

Java-8 onwards

Create a Stream out of the list, map each String of the Stream into YearMonth using a DateTimeFormatter , sort the Stream using Comparator#reverseOrder , map each YearMonth element of the Stream into the String using the same DateTimeFormatter and finally, collect the Stream into a List<String> .

Demo:

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

public class Main {
    public static void main(String args[]) {
        List<String> list = List.of("12-2014", "11-2012", "5-2014", "8-2012");
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M-uuuu", Locale.ENGLISH);
        List<String> sorted = 
                list.stream()
                    .map(s -> YearMonth.parse(s, dtf))
                    .sorted(Comparator.reverseOrder())
                    .map(ym -> dtf.format(ym))
                    .collect(Collectors.toList());

        // Display the list
        System.out.println(sorted);
    }
}

Output:

[12-2014, 5-2014, 11-2012, 8-2012]

ONLINE DEMO

Learn more about the modern Date-Time API * from Trail: Date Time .


* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring . Note that Android 8.0 Oreo already provides support for java.time .

尝试使用比较器或类似产品,并通过拆分来检查月份。

use a custom String comparator. http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

I can write the code out if you would like.

Here is some sudo code for now:

Arrays.sort(yearList, new Comparator<String>{

    //get the year and month of o1 and o2 as ints

    //if the years are different then return the difference of the years

    //if the years are the same then return the difference of the months

}

Try something like (untested):

private final static DateTimeFormatter YEAR_MONTH_FORMATTER = 
    DateTimeFormatter.ofPattern("MM-yyyy");

...

List<String> yearMonthStrings = ...;
List<YearMont> yearMonthList = yearMonthStrings.stream()
    .map(s -> YearMonth.parse(s, YEAR_MONTH_FORMATTER)
    .sorted()
    .collect(Collectors.toList());

// or
List<YearMont> yearMonthList = yearMonthStrings.stream()
    .map(s -> YearMonth.parse(s, YEAR_MONTH_FORMATTER)
    .sorted(Collections.reverseOrder())
    .collect(Collectors.toList());

Since you are using Java 7 you can take advantage of the Date class as well as the Comparator interface and its usage in a Set object like the treeSet for instance.

Here is an implementation of the Comparator interface to compare two dates

public class MonthYearComparator implements Comparator<Date> {

    public int compare(Date o1, Date o2) {
        // TODO Auto-generated method stub
        return -1*o1.compareTo(o2);
    }

}

And then here is how we could use it. First I will use a SimpleDateFormat class to parse your strings as dates. To do that I need to complete them and make them look as formated date strings. Since the day is irrelevant for the comparison I could take any day like "01".

Once I have turned them into Date objects I will add them to a instance of a TreeSet which is provided with a Comparator and they will automatically be sorted as I add them.

Then I can substract the part of the date that I need which will be a substring(3) of my date object after being formated as a string.

Here is the code (for demo sake I used the same datas that you provided as example):

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class MonthYearIssue {

    private List<String> listOfMonthYears = new ArrayList<String>();
    private static final String USED_DATE_FORMAT = "dd-MM-yyyy";
    SimpleDateFormat formatter = new SimpleDateFormat(USED_DATE_FORMAT);

    public void setUpMonthYearsList() {
        listOfMonthYears.add("12-2014");
        listOfMonthYears.add("11-2012");
        listOfMonthYears.add("5-2014");
        listOfMonthYears.add("8-2012");
    }

    public Date parseToDate(String monthYearString) throws ParseException {
        return formatter.parse("01-" + monthYearString);
    }

    public List<String> doSort() throws ParseException {

        List<String> result = new ArrayList<String>();
        Set<Date> dates = new TreeSet<Date>(new MonthYearComparator());

        for (String monthYearStr: listOfMonthYears) {
            dates.add(parseToDate(monthYearStr));

        }

        for (Object d: dates.toArray()) {

            result.add(formatter.format((Date)d).substring(3));
        }

        System.out.println(result);
        return result;

    }

    public static void main(String[] args) throws ParseException {
        MonthYearIssue issueSolver = new MonthYearIssue();
        issueSolver.setUpMonthYearsList();
        issueSolver.doSort();
    }

}

Here is the result:

[12-2014, 05-2014, 11-2012, 08-2012]

Date类是可比较的,因此您可以为数组中的每个String创建Date的实例,然后可以对它们进行排序

tl;dr

Never use the terrible legacy date-time classes such as Date & Calendar .

Always use their replacement, the modern java.time classes defined in JSR 310. Most of that functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport .

List < String > inputs = Arrays.asList( "12-2014" , "11-2012" , "5-2014" , "8-2012" );
DateTimeFormatter f = DateTimeFormatter.ofPattern( "M-uuuu" );

List < YearMonth > yearMonths = new ArrayList <>( inputs.size() );
for ( String input : inputs )
{
    try { yearMonths.add( YearMonth.parse( input , f ) ); } catch ( DateTimeException e ) { System.out.println( "ERROR - Faulty input: " + input ); }
}

Collections.sort( yearMonths , Collections.reverseOrder() );

Back-port java.time to Java 7

You said you are restricted to Java 7. While not built-in, most of the modern java.time functionality is available to you. Add the ThreeTen-Backport library to your project.

If using Maven, add this to your POM:

<dependency>
  <groupId>org.threeten</groupId>
  <artifactId>threetenbp</artifactId>
  <version>1.5.1</version>
</dependency>

The legacy date-time classes such as Date , Calendar , SimpleDateFormat are terribly flawed in design. They really are such an awful mess that adding a library to your project is well-worth the chore.

DateTimeFormatter

Set up your inputs.

By the way, your input text is using a custom format of M-YYYY. I suggest you educate the publisher of your data about the ISO 8601 standard for formatting date-time values exchanged textually. The standard format for a year-month is YYYY-MM.

Define a formatter to match your non-standard inputs.

List < String > inputs = Arrays.asList( "12-2014" , "11-2012" , "5-2014" , "8-2012" );
DateTimeFormatter f = DateTimeFormatter.ofPattern( "M-uuuu" );

YearMonth

Parse your inputs a YearMonth objects, adding each a new List .

If an input might be faulty, you can trap for DateTimeException to detect and handle such a problem.

List < YearMonth > yearMonths = new ArrayList <>( inputs.size() );
for ( String input : inputs )
{
    try { yearMonths.add( YearMonth.parse( input , f ) ); } catch ( DateTimeException e ) { System.out.println( "ERROR - Faulty input: " + input ); }
}

Collections.sort && Collections.reverseOrder

Sort your list. You want the latest first, so pass the optional second argument, a Comparator to reverse the sorting on natural-order.

Collections.sort( yearMonths , Collections.reverseOrder() );

Report our new list.

System.out.println( "yearMonths = " + yearMonths );

Example code

Pull all that code together.

package work.basil;

import org.threeten.bp.DateTimeException;
import org.threeten.bp.YearMonth;
import org.threeten.bp.format.DateTimeFormatter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Example parsing year-month values using ThreeTen-Backport library
 * giving us most of the Java 8+ *java.time* functionality.
 **/
public class App
{
    public static void main ( String[] args )
    {
        List < String > inputs = Arrays.asList( "12-2014" , "11-2012" , "5-2014" , "8-2012" );
        DateTimeFormatter f = DateTimeFormatter.ofPattern( "M-uuuu" );

        List < YearMonth > yearMonths = new ArrayList <>( inputs.size() );
        for ( String input : inputs )
        {
            try { yearMonths.add( YearMonth.parse( input , f ) ); } catch ( DateTimeException e ) { System.out.println( "ERROR - Faulty input: " + input ); }
        }

        Collections.sort( yearMonths , Collections.reverseOrder() );

        System.out.println( "yearMonths = " + yearMonths );
    }
}

When run.

yearMonths = [2014-12, 2014-05, 2012-11, 2012-08]


By the way, here is a Maven POM for this app, using the latest versions of various parts.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns = "http://maven.apache.org/POM/4.0.0"
         xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>work.basil</groupId>
    <artifactId>J7</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>J7</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>

    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

        <!--https://www.threeten.org/threetenbp/-->
        <dependency>
            <groupId>org.threeten</groupId>
            <artifactId>threetenbp</artifactId>
            <version>1.5.1</version>
        </dependency>


    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M5</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>3.0.0-M1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>3.0.0-M1</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.9.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.1.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM