I have an arraylist
with string values like
ArrayList<String> datestring=new ArrayList<String>();
datestring.add("01/21/2013 @03:13 PM");
datestring.add("01/21/2013 @04:37 PM");
datestring.add("01/21/2013 @10:41 AM");
datestring.add("01/21/2013 @10:48 AM");
datestring.add("01/22/2013 @06:16 AM");
datestring.add("01/22/2013 @06:19 AM");
datestring.add("01/21/2013 @05:19 PM");
datestring.add("01/21/2013 @05:19 PM");
Can any body help me on sorting the above list? So that the values are sorted according to AM and PM format.
The expected output after sorting should be
for (String s : datestring)
{
System.out.println(s);
}
.
01/21/2013 @10:41 AM;
01/21/2013 @10:48 AM;
01/21/2013 @03:13 PM;
01/21/2013 @04:37 PM;
01/21/2013 @05:16 PM;
01/21/2013 @05:19 PM;
01/22/2013 @06:16 AM;
01/22/2013 @06:19 AM;
ArrayList<String> datestring=new ArrayList<String>(); datestring. add("01/21/2013 @03:13 PM"); datestring. add("01/21/2013 @04:37 PM"); datestring. add("01/21/2013 @10:41 AM"); datestring.
The sort() method that is a part of Comparator mechanism of Collection class that sorts the data in decreasing order. If we want to achieve the aim in generic while considering the boundary condition where the objects to be sorted are user-defined, we can use Comparator interface.
Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.
try this
Collections.sort(datestring, new Comparator<String>() {
DateFormat f = new SimpleDateFormat("MM/dd/yyyy '@'hh:mm a");
@Override
public int compare(String o1, String o2) {
try {
return f.parse(o1).compareTo(f.parse(o2));
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
});
or with Java 8
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy '@'hh:mm a");
Collections.sort(datestring, (s1, s2) -> LocalDateTime.parse(s1, formatter).
compareTo(LocalDateTime.parse(s2, formatter)));
One can compare the List of date in string format by using the compareTo as given below.
ArrayList<String> datestring=new ArrayList<String>();
datestring.add("01/21/2013 @03:13 PM");
datestring.add("01/21/2013 @04:37 PM");
datestring.add("01/21/2013 @10:41 AM");
datestring.add("01/21/2013 @10:48 AM");
datestring.add("01/22/2013 @06:16 AM");
datestring.add("01/22/2013 @06:19 AM");
datestring.add("01/21/2013 @05:19 PM");
datestring.add("01/21/2013 @05:19 PM");
Collections.sort(datestring, new Comparator<String>() {
@Override
public int compare(String object1, String object2) {
return object1.compareTo(object2);
}
});
By using this you even dont have to parse the String into date
One option is to add the dates to a TreeMap, with the unformatted date as the key, and the formatted date as the value. Using a TreeMap will sort the values automatically.
private ArrayList<String> sortDates(ArrayList<String> dates) throws ParseException {
SimpleDateFormat f = new SimpleDateFormat("dd MMM yyyy");
Map <Date, String> dateFormatMap = new TreeMap<>();
for (String date: dates)
dateFormatMap.put(f.parse(date), date);
return new ArrayList<>(dateFormatMap.values());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With