简体   繁体   中英

What's the most efficient way to sort lists of date, week and month?

//dd/mm/yyyy
list<String> datesList

//weeks of year
list<int> weeksList

//month
list<int> monthsList

I would like to sort all of them in ascending order, what is the most efficient way to do this? (espcially the dates list, which is a string list with each item has the entry dd/mm/yyyy).

Since nobody's posting it, I'll have to: Collections.sort .
And for dates, as suggested by eumiro , the simplest approach is probably to convert them to Date object and then sort.

java.util.List<String> dates = Arrays.asList("05/07/2010", "12/12/1984", "28/01/2008");

System.out.println("unsorted dates = " + dates);

Collections.sort(dates, new Comparator<String>()
{
    @Override
    public int compare(String o1, String o2)
    {
        String y1 = o1.substring(6, 10);
        String y2 = o2.substring(6, 10);

        if (y1.equals(y2))
        {
            String m1 = o1.substring(3, 5);
            String m2 = o2.substring(3, 5);
            if (m1.equals(m2))
            {
                return m1.compareTo(y2);
            }
            String d1 = o1.substring(0, 2);
            String d2 = o2.substring(0, 2);
            return d1.compareTo(d2);
        }
        return y1.compareTo(y2);
    }
});

System.out.println("sorted dates = " + dates);

Here's how:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class DateSortExample
{
    public static void main(String[] args)
    {
        DateFormat format = new SimpleDateFormat("yyyy-MMM-dd");
        List<Date> values = new ArrayList<Date>();
        for (String arg : args)
        {
            try
            {
                Date value = format.parse(arg);
                values.add(value);
            }
            catch (ParseException e)
            {
                e.printStackTrace();
            }
        }

        System.out.println("before sort: " + values);
        Collections.sort(values);
        System.out.println("after  sort: " + values);
    }
}

Here's output:

before sort: [Mon Oct 18 00:00:00 EDT 2010, Mon Jan 04 00:00:00 EST 1999, Wed Dec 25 00:00:00 EST 1968]
after  sort: [Wed Dec 25 00:00:00 EST 1968, Mon Jan 04 00:00:00 EST 1999, Mon Oct 18 00:00:00 EDT 2010]

Create a comparator which converts every string date to a date object before comparison.

List<String> dates = Arrays.asList("05/07/2010", "12/12/1984", "28/01/2008");
final DateFormat fmt = new SimpleDateFormat("dd/mm/yyyy");
Collections.sort(dates, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        try {
            return fmt.parse(o1).compareTo(fmt.parse(o2));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
});
System.out.println(dates);

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