Results 1 to 11 of 11
  1. #1
    JohnDas is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default Sorting Gregorian Calendar?

    Hi there!

    I want to sort a series of objects, based on their GregorianCalendar values. (I need the oldest and most recent record given and the code that retrieves them may not give them to me already sorted, so I need to be sure)
    The objects comprise a value and the date it was recorded. I looked around for something similar and found getMinimum and getMaximum but I am not sure it will suit my needs. I just want to create a sorted array list of my objects with the Gregorian Calendar values sorted from oldest to newest..
    Any suggestions?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    GregorianCalendar is Comparable, so you can create a Comparator for your objects and inside that simply compareTo() on the calendars. Give the Comparator to the sort method.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Write a Comparator (see the API docs and tutorials) and use the before and after methods of Date.

    Edit: Okay, along the same lines. ;-)

  4. #4
    JohnDas is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    A ha!! Thanks a lot guys!! :D

  5. #5
    JohnDas is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Hello again guys!
    So, I have this kind of code :

    Java Code:
    if (CallerId == 1) { 
    				// Sort Dated Stuff in ascending order
    				 
    				//1st Select Appropriate Data
    				for (j=0; j<NumberOStuff; j++);
    				{
    				     if (stuff[j].getType().getCode().equals(2))
    				     {
    				    	 
    				SortedStuff.add(stuff[j]);
    				
    				     }
    				}
    				//Sort Stuff according to Date
    				int length=SortedStuff.size();
    				//for (k=0; k<length;k++)
    				//{
    					//SortedStuff.get(k).getDateTime();
    					Arrays.sort(SortedStuff, new Comparator())
    						...
    etc.. My "SortedsStuff" is an ArrayList which includes ..hmm.. stuff. A stuff object includes a DateTime instance.

    I will tell you what I have understood and please tell me if I am on the correct path :

    I ll do something like (ok pseudocode )

    if SortedStuff[i].before(SortedStuff[i+i]))
    then SortedStuff[i] remains where it is else it is replaced by i+1 ?
    OR the comparator works in another way? I ve read the API and ok, I dont get it quite clearly and examples I have found are somewhat contradicting at times.
    I dont want a solution out of the blue of course.. Just a bit of guidance.




    hmm now that I am reading this again, it seems very stupid..
    The thing is, the way the program is structured, I dont want to mess with new classes, only sort objects in my ArrayList in ascending order of date, (and then select the first and last).
    OK date implements comparable so I dont have to build a new comparator, so I ll work only with "before" and "after" etc?

    Sorry if I seem incoherent but I am way to perplexed right now..

    Thanks again


    UPDATE : Apart from the previous (which ok is stupid but I dont delete it in case I am wrong NOW) I thought of this solution :

    Java Code:
    static final Comparator <Stuff> Date_Order=new Comparator<Stuff>() {
    public int compare(Stuff a, Stuff b) { 
    return a.getDateTime().before(b.getDateTime());
    }
    
    and then
    
    collections.sort(SortedStuff,Date_Order);
    that sounds right??
    Last edited by JohnDas; 01-25-2011 at 01:08 PM.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    What does the Comparator interface description in the API docs say?

  7. #7
    JohnDas is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    What does the Comparator interface description in the API docs say?
    You mean that the interface description refers only to the "equals" method and doesnt say anything about before,after etc?

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    What about the compare method? Does that describe comparing all items in a list or something within that method call, or does that method describe comparing the "current" object with one other object? The latter, correct? That's because the sort method of Collections or Arrays does the rest.

  9. #9
    JohnDas is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Hmmm
    yes, so :

    Java Code:
    Code:
    static final Comparator <Stuff> Date_Order=new Comparator<Stuff>() {
    public int compare(Stuff a, Stuff b) { 
    return [B]a.getDateTime().compareTo(b.getDateTime());[/B]
    }
    
    and then
    
    collections.sort(SortedStuff,Date_Order);
    ?

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Well, capitalise collections and away you go.

  11. #11
    JohnDas is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Hehehe yep, that was something I DID see at once :D

    Million thanks and sorry for making stupid questions :D

Similar Threads

  1. help with Gregorian Calendar
    By fezman1337 in forum JCreator
    Replies: 3
    Last Post: 10-26-2010, 04:01 AM
  2. Gregorian calendar multiple dates
    By tirwit in forum New To Java
    Replies: 4
    Last Post: 09-17-2010, 03:10 PM
  3. Gregorian Calendar
    By bindhuuk4 in forum New To Java
    Replies: 1
    Last Post: 08-07-2009, 12:00 PM
  4. Help with gregorian calendar
    By osval in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 11:21 PM
  5. Gregorian calendar issue
    By orchid in forum New To Java
    Replies: 1
    Last Post: 05-16-2007, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •