Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Sorting String[][] arrays...

Thread view: 
Tripwire - 19 Feb 2005 02:52 GMT
Hi,

I've been trying to find a way to order an array of players, scores and
totals by lowest total first... down to highest at the bottom and I can't
seem to figure it out.  I've read about the the Array.sort(), but the
examples I have seen show it used with single arrays and not 2d arrays, so
I'm not sure if this would work.  I have come up with the following
solution, however this does not sort the array correctly, as if the third
player has the lowest score their record is not moved.  Below is an example
of the sort I am trying to perform:

class Sorting {
   public static void main(String[] args) {
   //array that stores the results of the game
   //name, 3 scores and total
   String[][] results = {{"Fred","1","2","1","4"},
      {"Betty","2","1","2","5"},
      {"Wilma","1","1","1","3"},
      {"Barney","2","1","1","4"}};
   int col=5;
//Print out array before changes
   System.out.println("Array Before Changes...");
   for (int x=0; x < results.length; x++) {
       for (int y=0; y < col; y++) {
           System.out.print(results[x][y] +'\t');
      } //inner for loop
      System.out.println('\n');
   } //Outer for loop
 System.out.println('\n');

   for (int x=0; x < results.length; x++) {
      col = col-1;
      String[] tmp = new String[5];
      int temp = results.length-1;
      for (int row=0; row < results.length; row++) {
      //check total column = col//
      //If bottom record's total is less than the top record
      if ((Integer.parseInt(results[temp][col])) <
(Integer.parseInt(results[row][col]))) {
       for (int a=0; a <= col; a++) {
         tmp[a] = results[row][a];  //Copy top record to temp array
         results[row][a] = results[temp][a]; //Move bottom record to top
         results[temp][a] = tmp[a]; //Move temp to bottom of array
       } //closes temp for loop
     } //Closes if
  } //Closes row for loop
 } //Closes for loop1

//Print out array after changes
   System.out.println("Array After Changes...");
   for (int x=0; x < results.length; x++) {
       for (int y=0; y < 5; y++) {
           System.out.print(results[x][y] +'\t');
       } //inner for loop
       System.out.println('\n');
   } //Outer for loop
 }
}

I'm sure I'm along the right lines... but I can't seem to figure that last
bit out... so any help with this would be greatly appreciated, as I have
been trying to work it out all day long and it's now nearly 3am!

Cheers

Elaine
IchBin - 19 Feb 2005 03:20 GMT
> Hi,
>
[quoted text clipped - 62 lines]
>
> Elaine

You could take a look at these other sort examples at:

 http://javaalmanac.com/cgi-bin/search/find.pl?words=sort

I like to use collects when sorting.

Signature

Thanks in Advance...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
 if there is any reaction, both are transformed.'
-  Carl Gustav Jung,  (1875-1961),  psychiatrist and psychologist

Anthony Borla - 19 Feb 2005 04:25 GMT
> Hi,
>
[quoted text clipped - 17 lines]
>        {"Barney","2","1","1","4"}};
>     int col=5;

<SNIP>

I'd be inclined to alter the design. You basically have a collection of
Players, each of whom has a a name, and a collection of Scores. This would
seem most appropriately modelled as *very roughly* the following:

   class Player
   {
        ...
        private String name;
        private int[] scores;
        ...
        public Player(String name, int[] scores)
        {
            ...
        }
        ...
        public void sortScores() { Arrays.sort(scores); }
        ...
    }
    ...
    ...
    Player[] players = new Player[]
        {new Player("Bob", new int[]{34, 56, 23}),
         new Player("Jill", new int[]{34, 56, 23}) };
    ...
    for (int i = 0; i < players.length; i++)
         players[i].sortScores();
    ...
    Arrays.sort(players);

Feel free to substitute collection classes for the arrays :) !

Sometimes just a little redesign can make the problem a whole lot easier to
solve, and, perhaps, even easy to later extend or change, if necessary.

I hope this helps.

Anthony Borla
Tripwire - 19 Feb 2005 14:09 GMT
> <SNIP>
>
[quoted text clipped - 36 lines]
>
> Anthony Borla

Hi Anthony,

Thanks for your reply.. Just some questions regarding the above solution...
sorry if they seem silly, it's just I am fairly new to Java and especially
object oriented stuff...

I actually need to store players scores without ordering them, then total
them and then organise the players according to lowest score (total) first.
So could I have an int variable total in the Player class, with a method to
total the scores??  But my question is then how do I organise the players in
the main class?  Would Arrays.sort(players) order them in this way???  I am
confused..!

TIA

Elaine
Tripwire - 19 Feb 2005 20:34 GMT
> Hi Anthony,
>
[quoted text clipped - 3 lines]
>
> <snipped>

Thanks for the replies.. I got it working as described in Anthony's post...
yipee!  I just needed to spend a little time working it out...

Thanks again

Elaine
Anthony Borla - 20 Feb 2005 04:34 GMT
> > Hi Anthony,
> >
[quoted text clipped - 7 lines]
> Anthony's post...  yipee!  I just needed to spend a little time
> working it out...

Sorry I couldn't reply sooner, and answer your earlier query.

However I'm glad you solved the problem, and I'm quite sure the experience,
though possibly painful, led to much learning. All the best for your future
studies.

Cheers,

Anthony Borla


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.