Solved

Sorting a multi-dimensional array

Posted on 2004-08-02
6
163 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: 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.

Question has a verified solution.

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

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This article explains the fundamentals of industrial networking which ultimately is the backbone network which is providing communications for process devices like robots and other not so interesting stuff.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

632 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