Sort List With String Dates
Solution 1:
You will need to implement a Comparator<String>
object that translates your strings to dates before comparing them. A SimpleDateFormat
object can be used to perform the conversion.
Something like:
classStringDateComparatorimplementsComparator<String>
{
SimpleDateFormat dateFormat = newSimpleDateFormat("dd-MM-yyyy");
public int compare(String lhs, String rhs)
{
return dateFormat.parse(lhs).compareTo(dateFormat.parse(rhs));
}
}
Collections.sort(arrayList, newStringDateComparator());
Solution 2:
here is a small example based on your input. This could be done with a few lines less, but I thought this would be better to understand. Hope it helps.
List<String> values = newArrayList<String>();
values.add("30-03-2012");
values.add("28-03-2013");
values.add("31-03-2012");
Collections.sort(values, newComparator<String>() {
@Overridepublic int compare(String arg0, String arg1) {
SimpleDateFormat format = newSimpleDateFormat(
"dd-MM-yyyy");
int compareResult = 0;
try {
Date arg0Date = format.parse(arg0);
Date arg1Date = format.parse(arg1);
compareResult = arg0Date.compareTo(arg1Date);
} catch (ParseException e) {
e.printStackTrace();
compareResult = arg0.compareTo(arg1);
}
return compareResult;
}
});
Solution 3:
At first already given answers are write, but that decisions they are not very fast.
Standard java Collections.sort use timsort. In average case it takes O(n*log(n)) comparations, so your custom comparator will call O(n*log(n)) times.
If performance is important for you, for example if you have large array you can do following things:
Convert string dates to int or long timestamps. This takes O(n) operation. And then you just sort array of longs or integers. Comparation of two atomic int are MUCH faster than any comparator.
If you want to get MORE speed for this sort, you can use use Radix sort (http://en.wikipedia.org/wiki/Radix_sort). I takes much memory but we can optimize it. As I see you don't need to specify time of day. So the range of values is not very big. At firts pass (O(n)) you can convert date to integer value, with next assumptions:
- 1970 01 01 is start date (or more specific time if you know it) and encode like 1
- lets max date is 2170 01 01
- all month have 31 day. So You get 31*12 = 372 values per year And than you can just sort array of integers using radix sort. Sorting values with 200 years range takes only 200 * 372 * 4 = 297600 bytes for merge sort array, but you get O(2 * n) complexisty.
Solution 4:
Here is a method I wrote for ordering an array of objects by their time parameter. It's done by comparing 2 String times every time, this can be easily adjusted for your date comparison by changing the pattern parameter to: "dd-MM-yyyy"
intrepositorySize= tempTasksRepository.size();
intinitialRepositorySize= repositorySize;
TasksoonTask= tempTasksRepository.get(0);
Stringpattern="HH:mm";
SimpleDateFormatsimpleDateFormat=newSimpleDateFormat(pattern);
for (int i= 0; i < initialRepositorySize; i++)
{
for (int j= 0; j < repositorySize; j++)
{
TasktempTask= tempTasksRepository.get(j);
try
{
DatetaskTime= simpleDateFormat.parse(tempTask.getTime());
DatesoonTaskTime= simpleDateFormat.parse(soonTask.getTime());
// Outputs -1 as date1 is before date2if (taskTime.compareTo(soonTaskTime) == -1)
{
soonTask = tempTask;
}
}
catch (ParseException e)
{
Log.e(TAG, "error while parsing time in time sort: " + e.toString());
}
}
tasksRepository.add(soonTask);
tempTasksRepository.remove(soonTask);
if ( tempTasksRepository.size() > 0 )
{
soonTask = tempTasksRepository.get(0);
}
repositorySize--;
Solution 5:
Try to use SimpleDateFormat with "d-MM-yyyy" pattern: 1. Create SimpleDateFormat 2. Parse listOfSData string array to java.util.Date[] 3. Sort date array using Arrays.sort 4. Convert Date[] to string array using the same SimpleDateFormat
Post a Comment for "Sort List With String Dates"