File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Sorting question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting question" Watch "Sorting question" New topic

Sorting question

Shane McKenzie

Joined: Jun 24, 2002
Posts: 10
Hi all,

I have to make a program that will take fouyr strings, received through JOptionPane, and print to screen these 4 strings in both ascending and descending alphabetic sequence. I'm sure it's simple, but it's giving me trouble. Ideally what I'd like to do is read in the strings, store them, then pass those 4 strings as arguments to construct a "word" object. Then call methods on this object which will fulfill the requirements.

It's these methods that are giving me trouble. I DON'T want to use an array at all, so havings aid that, I gather I'd use the method string1.compareToIgnoreCase(string2), which makes perfect sense. However, how do I make this work properly without simply hardcoding in a multitude of if statements that address every possible permutation? Though that would work, I can't think of anything uglier. ;-)

Any help here would be greatly appreciated. I haven't posted any code, as all I have is literally the method headers with no implementation code within them! Thanks in advance!


I've been trying to change the world for years, but they just won't give me the source code.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Shane,

How about you store the String objects in an ArrayList and use Collections.sort()?

For example: (this code must be compiled using jdk 1.5)

For more info on sorting using Collections, you can check out Sun's Java Tutorial:Lesson: Algorithms and Object Ordering .

[ November 13, 2004: Message edited by: Joyce Lee ]
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11923

is there a REASON why you don't want to use an array? To me, that's like saying "i want to build a house, but i don't want to use a hammer".

Sure, it can be done, but your handicapping yourself for no apparent reason.

I'm not saying you HAVE to use an array, or even that you SHOULD use one - i just don't understand your apparent predjudice against them...

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

Joined: Oct 30, 2001
Posts: 1970
Originally posted by fred rosenberger:
I'm not saying you HAVE to use an array, or even that you SHOULD use one - i just don't understand your apparent predjudice against them...

Because the homework assignment said don't use arrays?

If you know they're Strings and you know there's exactly four of them, a String[4] sounds like the right way to store the data. If there were different classes, an unknown or large number of them, Collections might be good. You can either use an unsorted Collection, like ArrayList, then sort it using methods on Collections, or use a sorted Collection, like TreeSet. Although it might be tricky to sort in both directions, with TreeSet.

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

Joined: Mar 13, 2004
Posts: 1272

[ November 15, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Shane McKenzie

Joined: Jun 24, 2002
Posts: 10
Hi all,

I like arrays as much as the next guy, the reason I don't want to use them in this case is that I'm doing the exercises from a java book, and this question appear at the end of chapter 4, BEFORE arrays and vectors and lists are ever introduced, which implied to me that one should use another method (pardon the pun) to execute the task. I'm assuming that the reason is to give someone a really good appreciation of arrays when they're brought into play, which is in chapter 5, much to my consternation. ;-)
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Sorting question
It's not a secret anymore!