Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I arrange multi-dimensional array by the String date value (MM/dd/yyyy) of the sub-arrays?

Posted on 2007-12-03
10
Medium Priority
?
241 Views
Last Modified: 2013-12-14
This question is very urgent.  I'm finalizing something that needs to be done in a few hours.  While I'm tinkering with it, I figured I would post it here.

I've got an multi-dimentsional arary that would expand out as follows:

People Array {
              0 =>  Array {
                                   0 => String:"Jon Doe"
                                   1 => String:"08/21/1980"
                                   2 => String:"OtherInfo1"
                                   3 => String:"OtherInfo2"
                                   4 => String:"OtherInfo3"
                                   }
              1 =>  Array {
                                   0 => String:"Jane Doe"
                                   1 => String:"09/11/1978"
                                   2 => String:"OtherInfo1"
                                   3 => String:"OtherInfo2"
                                   4 => String:"OtherInfo3"
                                   }
              2 =>  Array {
                                   0 => String:"Billy Doe"
                                   1 => String:"05/18/1992"
                                   2 => String:"OtherInfo1"
                                   3 => String:"OtherInfo2"
                                   4 => String:"OtherInfo3"
                                   }
}

Assume the data is already verified.  How do I put the People Array in order by Date (People[x][1])?  The data is received by the user and the length the the People array is undertermined.  So I'm thinking something along the lines of loop, but just not sure one the data best way to do the conversions.

Thanks in advance for any assistance.
0
Comment
Question by:VertigoRay
[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
  • 6
  • 4
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20397317
Use Arrays.sort with a Comparator
0
 
LVL 2

Author Comment

by:VertigoRay
ID: 20397340
Man I butchered the heck out of the last paragraph.  Here's a re-write:

Assume the data is already verified.  How do I put the People Array in order by Date (People[x][1])?  The data is received by the user and the length the the People array is undetermined.  So I'm thinking something along the lines of loop, but I am just not sure what the best way to do the data comparisons.

~Ray
http://www.vertigion.com
0
 
LVL 2

Author Comment

by:VertigoRay
ID: 20397356
CEHJ:  Can you expand on that?   Maybe a link to an example?
0
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!

 
LVL 2

Author Comment

by:VertigoRay
ID: 20397461
Before posting this, I had already found this in the docs:
http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#sort(T[],%20java.util.Comparator)

I read through it and the link to the comparator (http://java.sun.com/javase/6/docs/api/java/util/Comparator.html), but I'm still not clear on how to compare them.  That's why I posted this (phishing for an example).

Thanks again for the time!
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 20397513
The fact that you're writing procedural code (as opposed to OO) makes this trickier but the following should work:
Arrays.sort(array, new Comparator() {
	DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
	public int compare(Object o1, Object o2) {
		String[] s1 = (String[])o1;
		String[] s2 = (String[])o2;
		try {
			return df.parse(s1[1]).compareTo(df.parse(s2[1]);
		} catch (ParseException e) {
			e.printStackTrace();
			return Integer.MIN_VALUE;
		}
	}
});

Open in new window

0
 
LVL 2

Author Comment

by:VertigoRay
ID: 20398534
Forgot the closing ")" on the following line (fixed in my version:
return df.parse(s1[1]).compareTo(df.parse(s2[1]));

It is not working my particular code, but I think that it's something I need to work out.  I just need to fix a bug in my array creation which involves dynamically extending it.  I was under the impression that if I declared an array as shown below that it would dynamically expand the array as I in creased the value of the size.

The code below doesn't work ... but I was told that the logic of increasing the value of the "peopleCnt" should increase the length of the array.  The code below fails after the first pass.

Any thoughts?
int peopleCnt = 1;
String people[][];
people = new String[peopleCnt][];
 
for (<condition>)
{
   people[peopleCnt-1][0] = "<this String>";
   people[peopleCnt-1][1] = "<this String>";
   people[peopleCnt-1][2] = "<this String>";
   people[peopleCnt-1][3] = "<this String>";
   people[peopleCnt-1][4] = "<this String>";
   peopleCnt++;
}

Open in new window

0
 
LVL 2

Author Comment

by:VertigoRay
ID: 20398632
Correction ... line 3:
int peopleCnt = 1;
String people[][];
people = new String[peopleCnt][5];
 
for (<condition>)
{
   people[peopleCnt-1][0] = "<this String>";
   people[peopleCnt-1][1] = "<this String>";
   people[peopleCnt-1][2] = "<this String>";
   people[peopleCnt-1][3] = "<this String>";
   people[peopleCnt-1][4] = "<this String>";
   peopleCnt++;
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20399196
An array is inherently fixed size. You can't change it. You need to use a collection class if you want dynamic growth/shrinkage
0
 
LVL 2

Author Closing Comment

by:VertigoRay
ID: 31412390
I'll figure out collections.  I believe I'm out of bounds on the existing question, so I'll award you the earned points.  Thank you very much for your assistance!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20399466
:-)
0

Featured Post

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!

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

636 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