• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting question

 
Shane McKenzie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

shane
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .

Joyce
[ November 13, 2004: Message edited by: Joyce Lee ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ November 15, 2004: Message edited by: Mike Gershman ]
 
Shane McKenzie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. ;-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic