my dog learned polymorphism*
The moose likes Java in General and the fly likes Sorting an Array, Re-assign Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting an Array, Re-assign" Watch "Sorting an Array, Re-assign" New topic
Author

Sorting an Array, Re-assign

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
I have 2 arrays. Each array has the same number of elements as there is a 1-to-1 relationship. One holds people's names and the other a dollar amount. What is the easiest way to sort the array holding the people's names and then afterwards print out each name and corresponding dollar amount (the dollar amount and names must still be matched at the end). I'm doing this in a JSP. Thanks.

Rob
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Why not create a little class (I'll call it "PersonAmount") which holds a person/amount pair, implement Comparable on the class, then just use Arrays.sort() (or Collections.sort()) to sort them? One simple loop over your paired arrays could create the objects, then one line to sort them.


[Jess in Action][AskingGoodQuestions]
Francesco Bianchi
Ranch Hand

Joined: Jun 22, 2007
Posts: 62
Originally posted by Ernest Friedman-Hill:
Why not create a little class (I'll call it "PersonAmount") which holds a person/amount pair, implement Comparable on the class, then just use Arrays.sort() (or Collections.sort()) to sort them? One simple loop over your paired arrays could create the objects, then one line to sort them.



What Ernest means is something like this (sorry...didn't want to steal the answer...by you committed the post very little before I end up with the working code)

Another suggestion: this is the easiest way, the one with Comparable...I'd prefer using the sort() method version which accepts a comparator (maybe a Person innerclass?). I find it a better solution in term of flexibility for future implementations.

Cheers.



SCJP 5 & 6, SCWCD 5, SCBCD 5
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Bianchi --

That is indeed the kind of thing I meant. But here at the Ranch, we don't like to give out all the code for an answer for free -- it's better to help the person figure things out for themselves, because they learn more that way. Sometimes (I won't do that here) if we see someone posting code as an answer to something that's obviously a schoolwork question, the moderator will delete the code or delete your post altogether. So please, help the person learn to write the code themselves -- don't do their work for them. Thanks.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
Ernest,
Safe here in regards to school. 31, not much programming with Java. Working on this app that I'm getting totally frustrated with. Thanks Bianchi.

Rob
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11151
    
  16

even if it's not a school assignment, you're still going to learn more by trying, asking questions, and figuring it out yourself (with as much help as you need from here) than just being handed the solution.

Our goal is to make everyone better programmers, not be a 'solution provider'.

And, If i were 18 and in college, i'd say exactly what you did to make everyone think i wasn't


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
Lol. I guess you're right there Fred but the gray hairs wouldn't lie.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
Hi All,
I have an array that is declared as a string array. The first element is a string but the remainder is numeric (may be Integer or Double). I'm trying to sort on the second element through to the end of the array using a numeric sort. Below is the code I've been trying to use (after the Arrays.sort call the array gets set to all 0.0 values. Any help or suggestion on bettering the code would be appreciated. Thanks.

BufferedWriter fOut = new BufferedWriter(new FileWriter("C:/TestJSP2.txt"));
for (int tCnt=1;tCnt < elems.length-1; tCnt++) {
tmpSort[tCnt-1] = Double.parseDouble(elems2[tCnt]);
fOut.write(tmpSort[tCnt-1]+"\n");
}
Arrays.sort(tmpSort);
for (int tCnt=1;tCnt < elems.length-1; tCnt++) {
fOut.write(tmpSort[tCnt-1]+"\n");
}
fOut.close();


Rob
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
Never mind I see what the sort is doing. I declared the double array to be 1024 (maximum to hold values coming in). Only 10 values were really going in, 1 below 0 and 9 above. After sorting I was expecting the negative value and then the 9 values afterwards but I was getting the negative value and 9 0s. Here's a question I have related to arrays:

In perl when I add 10 values the array is 10 values long. In Java is there a flexible way to add values to an array and the array only to be that size? Right now I'm setting the array to be 1024 only to make sure it handles the number of values coming in. I like the way I'm able to add to, delete from, etc.., arrays in perl and it resizes the arrays. Can anyone suggest the most flexible way of doing this in Java?

Thanks.

Rob
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
What programming language did you come from? Fortran, maybe?

... sorry ...

In Java, don't use arrays any more than you really have to. Collections are much better. Arrays cannot be resized. Collections can. Appropriate Collection classes might be ArrayList or LinkedList; if you have only a few entries, it doesn't matter which you choose, but if you have many thousands, it can affect performance a lot.

As others have already said, don't use parallel arrays to store several pieces of data about one thing. Use a class to encapsulate all the data about one thing. Make your class implement Comparable, so Collections of instances can be sorted.

So, your application should use a single Collection of objects of a class that encapsulates all the data for one entry. For extra points, use a typed Collection, via Java Generics, but if all this stuff is new to you, maybe skip that to start with.
[ June 25, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
In addition to the last post, I split a string into a string array and do a length and it says length of 10. I copy those values into the double loop via for loop and then do a sort using 1 as the start and the length of the string array (i.e. 10) as the end of the sort. After the sort I do length for the double array I get 1024. Does anyone have a good explanation to why and what can I do to grab the "true" length? Thanks again.

Rob
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sorting an Array, Re-assign
 
Similar Threads
Array of Strings
Understanding Multi-dimension Arrays
How to identify the whole number in code
retrieve a variable that matches a string?
Comparing integer element data in Arrays