0

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.

Kai
39.7k14 gold badges91 silver badges105 bronze badges
asked Jan 9, 2012 at 14:13
4
  • 1
    Show us what you tried, and explain us why you store dates as Strings rather than storing them as Dates. Commented Jan 9, 2012 at 14:15
  • can you show what you've tried already.. Commented 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 sorting Commented Jan 9, 2012 at 14:17
  • pretty much a duplicate of stackoverflow.com/questions/3342517/… Commented Jan 9, 2012 at 14:19

8 Answers 8

6

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;
 }
 });
answered Jan 10, 2012 at 7:38
0
3

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);
 }
}
answered Jan 9, 2012 at 14:17
2

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.

answered Jan 9, 2012 at 14:17
1

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());
answered Jan 9, 2012 at 14:34
0

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.

answered Jan 9, 2012 at 14:16
0

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>;
 }
}
answered Jan 9, 2012 at 14:17
0

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.

answered Jan 9, 2012 at 14:19
0

Follow instructions from here, just use a method from the Date class to compare dates

answered Jan 9, 2012 at 14:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.