Solved

saving and sorting results in array - date sort

Posted on 2004-08-05
10
203 Views
Last Modified: 2010-03-31
Hello All,

This should be fairly easy for someone:

I get back from a query results: A date and a string (or many strings). I want to save these to an array, sort by date to get the latest date, and also remove any dates that are within one week of each other.

How do I do that in Java?

Thanks,
steeldawn
0
Comment
Question by:steeldawn
10 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 43 total points
ID: 11729149
Do you mean a Date object and String objects? If you save them into an array you will have to convert either Strings into Dates or the other way round to do the comparison.

If you know before hand the number of results you can use an array, otherwise you will have to use a collection that expands dynamically.

Doing date comparison shouldn't be difficult. What I would do would be to get th emilliseconds and then check to see if the difference between two dates is > than the milliseconds within a week. Then you will know if they are within one week of each other or not.

If you post some code we can help you more.
0
 

Author Comment

by:steeldawn
ID: 11729251
Some more info:

I have an event array that I loop through:
for (int i = 0; i < event.length; i++)
{
WebformsLabEvent wfle = new WebformsLabEvent(event[i]);
WebformsLabObs wflo = wfle.findObs("5759");
if (wflo != null)
{
date = wfle.getCollectionDateTime();      //Get the date of the lab
result = wflo.getObsValue();      //Get the lab value
units = wflo.getUnits();      //Get the units of the lab
}
The display needs to look like this. I can format the date to do this after sorting with the full date:
Lab Name
04/30/2003    6.5 %
02/15/2004    7%
02/15/2004    3%
02/16/2004    4.5%
05/23/2002    6%
But I really want reverse date order and no results within one week of each other so it should look like this after:

Lab Name
02/16/2004  4.5%
4/30/2003    6.5%
5/23/2002    6%

Thanks.
 
0
 

Author Comment

by:steeldawn
ID: 11729268
Sorry, forgot to say that date is a Date, result and units are Strings, and I do not know the length of the results I will get back beforehand.

Thanks.

0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11729627
listening
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11729871
The following code will help you.  But you need to do calculations for your result and units related display

Below only checks the date and string comparision

if you want to calculate on float values then

you need to create the DateObject instance some thing like this DateObject obj=new DateObject(new java.util.Date(),new Double(6.5)) something like this.

Basically you need to have a comparator for this

Best Regards
Sudhakar



import java.util.*;


class SortDates implements Comparator
{
     
      public int compare(Object o1,Object o2)
      {
            DateObject obj=(DateObject)o1;
            DateObject obj1=(DateObject)o2;
             
                    Date d1=obj.date;
                    Date d2=obj1.date;
                    String s1= obj.string;
                    String s2=obj1.string;      
                  
                    if(d1.getTime()>d2.getTime())
                      return 1;
             
                    if(d1.getTime()<d2.getTime())
                        return 0;

                        return s1.compareTo(s2);
                    
      }
     
}

class DateObject
{
   Date date;
   String string;      
   DateObject(Date date,String string)
   {
      this.date=date;
      this.string=string;
   }
}

public class SortDatesTest
{
      public static void main(String s[])
      {
            Date d=new Date();//same dates
            Date d1=new Date(d.getTime()+24*60*60*1000); //one day after
            Date d2=new Date();//same dates
            Date d3=new Date(d.getTime()-24*60*60*1000);//1 day before
            DateObject obj[]=new DateObject[4];
            obj[0]=new DateObject(d,"6");
            obj[1]=new DateObject(d1,"4");
            obj[2]=new DateObject(d2,"5");
            obj[3]=new DateObject(d3,"5");
            //before
            System.err.println("Before");
            for(int i=0;i<obj.length;i++)
            {
                  System.err.println("Date :"+obj[i].date +" String "+obj[i].string);
            }
            Arrays.sort(obj,new SortDates());
            //after
            System.err.println("After");
            for(int i=0;i<obj.length;i++)
            {
                  System.err.println("Date :"+obj[i].date +" String "+obj[i].string);
            }

      }      
}      

Best Regards
Sudhakar
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 41 total points
ID: 11729976
for elimimating same day values, you need to do some thing like this after sorting.

      for(int i=obj.length-1;i>0;i--)
            {
                  
                  try
                  {
                        if(!(obj[i].date.getTime()==obj[i-1].date.getTime()))
                        {
                              System.err.println("Date :"+obj[i].date +" String "+obj[i].string);
                              //store the results in new array
                        }
                  }
                  catch(Exception e1){}
            }
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 41 total points
ID: 11734503
select somedate, somestring1, somestring2 from sometable order by somedate descending
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11919610
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 70
Java DateChooser? 3 29
custom annotations 9 33
factorial example 4 35
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now