Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get the oldest timestamp record in an array list

 
guestin loy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Regards
 
Joe Ess
Bartender
Posts: 9279
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch.
We will not do your work for you but we are glad to help. What have you tried so far?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how you can calculate the minimum of a list of numbers? This is basically the same problem.
 
Paul Yule
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48909
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ove Lindstr�m:
Collection.sort(List<E> aList)[/QB]


Just a small correction of a typo here.
It should be Collections.sort(List<E> aList)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic