Solved

saving and sorting results in array - date sort

Posted on 2004-08-05
10
201 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

708 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

13 Experts available now in Live!

Get 1:1 Help Now