-5

I have arraylist having document No and created Date, the date format in String. I have to sort the arrayList with latest created date. Thanks in advance.

Date(String) format: 1/1/2015

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Lavanya
  • 89
  • 1
  • 10
  • What does it have to do with JSF? – Tiny Apr 02 '15 at 05:32
  • Did you try anything so far? Are you familiar with `Comparator` usage? – Ria Apr 02 '15 at 05:40
  • Don't stringify everything in Java side. This would only end up in headaches. Just store dates as dates, not as strings. Strings should only be used when taking it as input from humans (and should be converted immediately before putting in model) or presenting it to humans (and should be converted only during presentation). – BalusC Apr 02 '15 at 06:16

4 Answers4

0

There are plenty of StackOverflow answers that can already help you. For instance:

I'd also recommend you'd look into Collections.sort (and implementing Comparator).

Community
  • 1
  • 1
Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
0

You must use Collections.sort() method and pass the list as argument. The list will be ordered according to the natural ordering of its element. You can have more info here: http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

Elvermg
  • 427
  • 6
  • 12
0
public static void main(String[] args) {
    List dateList = new ArrayList();

    dateList.add(convertToString("1/1/2015"));
    dateList.add(convertToString("2/1/2015"));
    dateList.add(convertToString("1/2/2015"));
    dateList.add(convertToString("3/1/2015"));
    dateList.add(convertToString("1/4/2015"));
    dateList.add(convertToString("4/1/2015"));
    dateList.add(convertToString("2/2/2015"));
    dateList.add(convertToString("2/4/2015"));
    dateList.add(convertToString("4/4/2015"));

    Collections.sort(dateList);
    System.out.println(dateList);

}

public static String convertToString(String dateInString){

    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    Date date = null;
    try {

        date = formatter.parse(dateInString);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return formatter.format(date);
}

please have a look at the above code, I think it should solve your issue.

Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
0

This is a very easy way of implementing it, using apache-commons DateUtils.

    List<String> dates = new ArrayList<String>();
    dates.add("3/1/2015");
    dates.add("1/4/2015");
    dates.add("1/1/2015");
    final String[] formats = new String[] { "dd/MM/yyyy" };
    Collections.sort(dates, new Comparator<String>() {

        @Override
        public int compare(String d1, String d2) {
            try {
                Date date1 = DateUtils.parseDate(d1, formats);
                Date date2 = DateUtils.parseDate(d2, formats);
                return date1.compareTo(date2);
            } catch (ParseException e) {
                // do something to treat this error
                return 1;
            }
        }
    });
Ria
  • 1,900
  • 4
  • 14
  • 21