Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting a multi-dimensional array

Posted on 2004-08-02
6
Medium Priority
?
166 Views
Last Modified: 2010-04-01
Hello,


I've got a little problem.

I'm fetching data from different oracle tables and databases. I'm generating a text from the data and have a timestamp.

So first i fetch data from database 1, then database 2 and then database 3.

So i've got something like this

DateField                                             Textfield
------------------                         ----------------------------------------------------
2004-05-15                                 Text from oracle table 1
2004-01-18                                  Text from oracle table 1  
2003-03-12                                  Text from oracle table 2
2004-03-15                                 Text from oracle table 2
2004-08-18                                  Text from oracle table 3  
2003-06-12                                  Text from oracle table 3

Now i want to display the textfield contents chronologically.
I think it's best to fill an array with the content and then sort them

how do i do this ?
0
Comment
Question by:Xyleen
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11691873
Arrays.sort(array, new Comparator()
{
   public void compare(Object a, Object b)
   {
      Object[] rowa = (Object[] a;
      Object[] rowa = (Object[] a;
      return   // do your required comparison here
   }
});
0
 

Author Comment

by:Xyleen
ID: 11691953
Thanks for your help but this doesn't give me a clue of how i can do it

I haven't found how i can create and fill a multidimensional array either.
0
 
LVL 92

Expert Comment

by:objects
ID: 11691970
Object[][] array = new Object[rows][columns];
array[0][0] = "row 0, col 0";
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Xyleen
ID: 11692581
sorry but this is way to cryptic for me
0
 
LVL 3

Expert Comment

by:haobaba1
ID: 11696545
Create an object, call it record, with a time stamp and a String which will hold your timestamp and data from the individual tables.

Then make a recordComparator object that implements the Comparator interface. It needs a function that looks like the following:

int compare(Object o1, Object o2){
 }

inside this function cast to your datatype and then compare the date fields like so:

int compare(Object o1, Object o2){
         record rec1=(record) o1;
         record rec2=(record)o2;
        int i=0;
        if(rec1.getTimeStamp()== rec2.getTimeStamp()){
           i=0;
        }
        else if(rec1.getTimeStamp() < rec2.getTimeStamp()){
           i= -1;
        }
        else{
            i=1;
        }
        return i;
}

This function returns zero if the objects are equal and less than zero if the first object is less than the second and greater than zero if the first object is greater than the second.

next create an ArrayList and add all of you record objects to the array list and sort it using the comparator.

ArrayList recordArray;
Collections.sort(recordArray, new recordComparator());

your array is now sorted.


0
 
LVL 92

Accepted Solution

by:
objects earned 720 total points
ID: 11849801
Object[][] array = new Object[rows][columns];
for (int i=0; i<rows; i++)
{
   array[i][0] = getDate(i);
   array[i][0] = getString(i);
}

Arrays.sort(array, new Comparator()
{
   public void compare(Object a, Object b)
   {
      Object[] rowa = (Object[]) a;
      Object[] rowb = (Object[]) b;
      return   ((Comparable)rowa[0]).compareTo(rowb[0])
   }
});
0

Featured Post

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.

Question has a verified solution.

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

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Ranking ecommerce websites is a vital process. You need to have a strong SEO (Search Engine Optimization) strategy. If you don’t have one, you are losing out on brand impressions, clicks and sales. Check this guide on how to improve website traffic …
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses

885 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