Link to home
Create AccountLog in
Avatar of steeldawn
steeldawn

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of steeldawn
steeldawn

ASKER

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.
 
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.

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
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.