Solved

Sorting a multi-dimensional array

Posted on 2004-08-02
6
158 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A Short Story about the Best File Recovery Software – Acronis True Image 2017
What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

17 Experts available now in Live!

Get 1:1 Help Now