Solved

Sorting a multi-dimensional array

Posted on 2004-08-02
6
161 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
There's a lot of hype surrounding blockchain technology. Here's how it works and some of the novel ways it' s now being used - including for data protection.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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