This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
how to get the oldest timestamp record in an array list
guestin loy
Greenhorn
Joined: Oct 09, 2008
Posts: 5
posted
0
Hi, I have an arraylist. now i want to get the record with the oldest timestamp from all the records present in the arraylist. example: arraylist[] record1 : Thu Oct 09 20:51:03 GMT+05:30 2008 record2: Fri Oct 09 20:51:03 GMT+05:30 2009 record3: Thu Oct 10 20:51:03 GMT+05:30 2008 record4: Thu Dec 09 20:51:03 GMT+05:30 2006
The required output should be record4:Thu Dec 09 20:51:03 GMT+05:30 2006. could you please suggest any solution for this?
Originally posted by fred rosenberger: to figure out how to do it in java, first think about how you would do it yourself. try and write out the steps you would take.
This is good advice. Write out the steps, then replace each step with functional code. When you get to hicups on actual syntax and questions on whether java has a function 'ready-made' to do a certain step people here will be more than glad to help. Showing any attempt...even if it's just the psuedo'd non-sense makes us much more likely to help with code.
guestin loy
Greenhorn
Joined: Oct 09, 2008
Posts: 5
posted
0
Thank you all. few things are there in my arraylit object. Array list is defined in some object say (objDepartmentDetails). and that Arraylist (say alDept) contains Department objects.The Department object has the fields DeptNo and DeptDate.The getter method for DeptNo is getDeptNum and for the DeptDate is getDeptDate.
and i calling this object(objDepartmentDetails) from other class. I tried few things. 1. I converted date to Milisecond and then used this min_time = Array[0].getMillisecond ;max_time =Array[0].getMillisecond; for (int i = 0; i < Array.length; i++) { if (Array[i].getMillisecond < min_score) { min_time = Array[i].getMillisecond; } else { if (Array[i].getMillisecond > max_score) { max_time = Array[i].getMillisecond; }
but i want to use if any already defined method is available 'cos i have to use the similar logic for 4-5 methods so, 2. I thought of using Collections.sort(); but its not accepting my Arraylist.
2. Then i tried with getSortedList(ArrayList alObject, String getMethod,String dataType,char order) but its not recognising the getDeptDate().
i am now clueless.Could you correct me and help in giving me right direction?
Regards.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32668
4
posted
0
Drop the else in else if; it is quite possible that the same value can be smaller than the minimum and greater than the maximum; in fact if you set your initial minimum and maximum correctly that should happen with the first value. What are you setting your initial minimum and maximum to? What you actually need to do is record the index where you found the minumum and maximum; then you can dispense with sorting.
guestin loy
Greenhorn
Joined: Oct 09, 2008
Posts: 5
posted
0
i mistakenly give some other variable name. i correctd the same in bold. i'm initializing it with the very firts record. min_time = Array[0].getMillisecond ;max_time =Array[0].getMillisecond; for (int i = 0; i < Array.length; i++) { if (Array[i].getMillisecond < min_time) { min_time = Array[i].getMillisecond; } else { if (Array[i].getMillisecond > max_time) { max_time = Array[i].getMillisecond; }
as per your suggestion, i can take the Index where the min time and max time is present and then can retrive that element from the array list by avoidign sorting.
Could you please suggest any other inbuilt function to get the sort for the list? Please note that the list here is JAXB generated list.
One small improvement (apart from losing the else) that will prevent errors if your array is empty:
The idea here is that the minimum and maximum over an empty list or array are positive infinity and negative inifinity respectively. Since those are not valid for long Long.MAX_VALUE and Long.MIN_VALUE are the closest matches.
In my humble opinion, you are not thinking in an object oriented way...
If you go this strings to sort according to the date:
Then I would attack the problem like this.
- I got a string that is has a description, a semicolon as delimiter and a date string. - Lets split the string into two, the description and the date string. - Hmm.. how can I convert the date string into something comparable? - Oh, I can use the SimpleDateFormat and use a format description that looks something like this (but not exact) ""EEE MMM d HH:mm:ss Z yyyy" and parse the string into a Date-object. - Then I put the date-object into a List of some sort. - When I'm done parsing all the string, let's use Collection.sort(List<E> aList) on that list.
Well.... yes, you have to do something to keep track of the date-record relation...