Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sorting a multi-dimensional array

Posted on 2004-08-02
6
Medium Priority
?
164 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
Independent Software Vendors: 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 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

Technology Partners: 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!

Question has a verified solution.

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

Want to know how to use Exchange Server Eseutil command? Go through this article as it gives you the know-how.
On September 18, Experts Exchange launched the first installment of the Help Bell, a new feature for Premium Members, Team Accounts, and Qualified Experts. The Help Bell will serve as an additional tool to help teams increase question visibility.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

670 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