Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

saving and sorting results in array - date sort

Posted on 2004-08-05
10
Medium Priority
?
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 172 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11729627
listening
0
 
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 164 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 164 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

618 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