I have a custom object whose structure is
public String name;
public int type;
public String createdOn;
createdOn
is date and its format is : 2011年12月16日T23:27:27+0000
I have stored multiple objects of this type in ArrayList
and now I want to sort them according to there creation date and time.
I have tried using Collections.sort(....)
, but no suitable result.
-
1Show us what you tried, and explain us why you store dates as Strings rather than storing them as Dates.JB Nizet– JB Nizet2012年01月09日 14:15:29 +00:00Commented Jan 9, 2012 at 14:15
-
can you show what you've tried already..Anantha Sharma– Anantha Sharma2012年01月09日 14:16:15 +00:00Commented Jan 9, 2012 at 14:16
-
use Dates as Dates rather then String or better yet, modify your object and add another value (if possible derived from data/time) on which you can perform sortingJohnydep– Johnydep2012年01月09日 14:17:46 +00:00Commented Jan 9, 2012 at 14:17
-
pretty much a duplicate of stackoverflow.com/questions/3342517/…aaazalea– aaazalea2012年01月09日 14:19:07 +00:00Commented Jan 9, 2012 at 14:19
8 Answers 8
Doing some R&D after getting answers, I solved the sorting thing, here is the code to sort array list by date.
Collections.sort(arrlst,new Comparator<T>() {
public int compare(T lhs, T rhs) {
try {
SimpleDateFormat dateFormatlhs = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date convertedDatelhs = dateFormatlhs.parse(lhs.feedCreatedTime);
Calendar calendarlhs = Calendar.getInstance();
calendarlhs.setTime(convertedDatelhs);
SimpleDateFormat dateFormatrhs = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date convertedDaterhs = dateFormatrhs.parse(rhs.feedCreatedTime);
Calendar calendarrhs = Calendar.getInstance();
calendarrhs.setTime(convertedDaterhs);
if(calendarlhs.getTimeInMillis() > calendarrhs.getTimeInMillis())
{
return -1;
}
else
{
return 1;
}
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
});
You have to use Collections.sort(List<T> list, Comparator<? super T> c)
and implement a comparator like
Comparator c = new Comparator<YourObject>() {
public int compare(YourObject o1, YourObject o2) {
return o1.createdOn.compareTo(o2.createdOn);
}
}
You will need to create your own Custom Comparator (Take a look here). You will most likely have to parse the strings back to dates. Take a look at this previous SO post to see how you can compare dates.
You should use a Comparator or implement Comparable in your custom object.
Implementing a Comparable you define a natural ordering for your objects. In the Comparable.compareTo() method you have to compare the current object with another one and return
- a negative integer if the current object is "less than" the other one;
- zero if the current object is "equal" to the other one;
- a positive integer if the current object is "greater than" the other one.
An example:
public class CustomObject implements Comparable<CustomObject> {
@Override
public int compareTo(CustomObject otherOne) {
// Compare the current object with the otherOne and return an
// appropriate integer
return 0;
}
}
// To sort
Collections.sort(listOfCustomObjects);
From the other side, implementing a Comparator you can specify an ordering criteria outside the code of the custom Object. Using the appropriate Collections.sort() method, you can specify the order for the objects in a collection. The Comparator.compare() method follows the same rules explained above.
An example:
public class CustomObjectComparator implements Comparator<CustomObject> {
@Override
public int compare(CustomObject a, CustomObject b) {
// Compare a with b and return an appropriate integer
return 0;
}
}
// To sort
Collections.sort(list, new CustomObjectComparator());
For a Custom object, you need a custom Comparator or you need to make the class comparable.
You could convert the date string each time, or you could just compare strings (provided the time zone doesn't change)
As has been suggested, storing dates as a Date or a long is likely to be a better choice.
Have you tried implementing your custom Comparator
?
new Comparator<CustomObject>() {
public int compare(CustomObject o1, CustomObject o2) {
//compare by date
return <-1 or 0 or 1>;
}
}
Your class should implement interface Comparable
. In this case you can implement compareTo()
method according to your needs.
Alternatively you can implement your custom comparator:
class MyCreationDateComparator implements Comparator<MyClass> {
public int compare(MyClass o1, MyClass o2) {
// implement your logic here.
}
}
Now use version of Collections.sort()
that accepts custom comparator. If your comparator is simple you can use anonymous inner class.
Follow instructions from here, just use a method from the Date class to compare dates
Explore related questions
See similar questions with these tags.