Results 1 to 11 of 11
Thread: Sorting Gregorian Calendar?
- 01-11-2011, 08:54 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
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?
- 01-11-2011, 09:09 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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.
- 01-11-2011, 09:10 AM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Write a Comparator (see the API docs and tutorials) and use the before and after methods of Date.
Edit: Okay, along the same lines. ;-)
- 01-11-2011, 10:59 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
A ha!! Thanks a lot guys!! :D
- 01-25-2011, 12:39 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
Hello again guys!
So, I have this kind of code :
etc.. My "SortedsStuff" is an ArrayList which includes ..hmm.. stuff. A stuff object includes a DateTime instance.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()) ...
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 :
that sounds right??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);Last edited by JohnDas; 01-25-2011 at 01:08 PM.
- 01-25-2011, 01:08 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
What does the Comparator interface description in the API docs say?
- 01-25-2011, 01:13 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
- 01-25-2011, 01:16 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 01-25-2011, 01:18 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
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);
- 01-25-2011, 01:23 PM #10
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Well, capitalise collections and away you go.
- 01-25-2011, 01:24 PM #11
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
help with Gregorian Calendar
By fezman1337 in forum JCreatorReplies: 3Last Post: 10-26-2010, 04:01 AM -
Gregorian calendar multiple dates
By tirwit in forum New To JavaReplies: 4Last Post: 09-17-2010, 03:10 PM -
Gregorian Calendar
By bindhuuk4 in forum New To JavaReplies: 1Last Post: 08-07-2009, 12:00 PM -
Help with gregorian calendar
By osval in forum New To JavaReplies: 2Last Post: 08-06-2007, 11:21 PM -
Gregorian calendar issue
By orchid in forum New To JavaReplies: 1Last Post: 05-16-2007, 06:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks