• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

saving and sorting results in array - date sort

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
steeldawn
Asked:
steeldawn
3 Solutions
 
girionisCommented:
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
 
steeldawnAuthor Commented:
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
 
steeldawnAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sudhakar_koundinyaCommented:
listening
0
 
sudhakar_koundinyaCommented:
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
 
sudhakar_koundinyaCommented:
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
 
CEHJCommented:
select somedate, somestring1, somestring2 from sometable order by somedate descending
0
 
sudhakar_koundinyaCommented:
thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now