Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Trying to sort the values of a two dimensional array in ascending order.

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to sort the values of a two dimensional array in ascending order.

    Right now, the program is determining and outputting the medal standings in descending order, however, I would like to determine and output the medal standings in ascending order. I'm not sure how to change my program in order to accomplish this task, if anyone is able to guide me onto the right path, that would be much appreciated.
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    public class OlympicMedals2D {
    public static void main (String [] args) {

    // Initializing variables.

    String country[] = {"Norway", "Germany", "Canada", "United States", "Netherlands", "South Korea", "Russia", "Switzerland", "France", "Sweden", "Austria", "Japan", "Italy", "China", "Czech Republic", "Finland", "Britain", "Belarus", "Slovakia", "Australia", "Poland", "Slovenia", "Spain", "New Zealand", "Hungary", "Ukraine", "Belgium", "Kazakhstan", "Latvia", "Liechtenstein"};
    String countryMostTotalMedals = null;
    String countryMostGoldMedals = null;
    String countryMostSilverMedals = null;
    String countryMostBronzeMedals = null;
    int medals[][] = {{14, 14, 11}, {14, 10, 7}, {11, 8, 10}, {9, 8, 6}, {8, 6, 6}, {5, 8, 4}, {2, 6, 9}, {5, 6, 4}, {5, 4, 1}, {7, 6, 1}, {5, 3, 6}, {4, 5, 4}, {3, 2, 5}, {1, 6, 2}, {2, 2, 3}, {1, 1, 4}, {1, 0, 4}, {2, 1, 0}, {1, 2, 0}, {0, 2, 1}, {1, 0, 1}, {0, 1, 1}, {0, 0, 2}, {0, 0, 2}, {1, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {0, 0, 1}, {0, 0, 1}};
    int totalMedals[] = new int [medals.length];
    int mostTotalMedals = 0;
    int greatestTotalMedals = 0;
    int mostGoldMedals = 0;
    int mostSilverMedals = 0;
    int mostBronzeMedals = 0;
    int medalCounter = 0;
    int row;
    int column;

    // Determining country with highest total, gold, silver, and bronze medal count.

    for (row = 0; row < medals.length; row += 1) {

    for (column = 0; column < medals[row].length; column += 1) {

    totalMedals[row] = totalMedals[row] + medals[row][column];

    if (mostGoldMedals < medals[row][0]) {
    mostGoldMedals = medals[row][0];
    countryMostGoldMedals = country[row];
    }
    if (mostSilverMedals < medals[row][1]) {
    mostSilverMedals = medals[row][1];
    countryMostSilverMedals = country[row];
    }
    if (mostBronzeMedals < medals[row][2]) {
    mostBronzeMedals = medals[row][2];
    countryMostBronzeMedals = country[row];
    }
    }

    if (mostTotalMedals < totalMedals[row]) {
    mostTotalMedals = totalMedals[row];
    countryMostTotalMedals = country[row];
    }
    }

    // Outputting country with highest total, gold, silver, and bronze medal count.

    System.out.println("The country with the most total medals is " + countryMostTotalMedals + " with " + mostTotalMedals + ".");
    System.out.println("The country with the most gold medals is " + countryMostGoldMedals + " with " + mostGoldMedals + ".");
    System.out.println("The country with the most silver medals is " + countryMostSilverMedals + " with " + mostSilverMedals + ".");
    System.out.println("The country with the most bronze medals is " + countryMostBronzeMedals + " with " + mostBronzeMedals + ".");
    System.out.println("\nMedal Standings");

    // Determining medal standings in descending order (I would like to be able to determine the medal standings in ascending order).

    for (row = 0; row < medals.length; row += 1) {

    for (column = 0; column < medals.length; column += 1) {

    if (totalMedals[column] > greatestTotalMedals) {
    greatestTotalMedals = totalMedals[column];
    medalCounter = column;
    }
    }

    // Outputting medal standings in descending order (I would like to be able to output the medal standings in ascending order).

    System.out.println(country[medalCounter] + "\t" + totalMedals[medalCounter]);

    totalMedals[medalCounter] = 0;
    medalCounter = 0;
    greatestTotalMedals = 0;
    medalCounter = 0;
    }
    }
    }
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Last edited by SirPaco; October 25th, 2018 at 01:58 PM.

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Trying to sort the values of a two dimensional array in ascending order.

    You're not really sorting anything. For a simple sort try Googling - bubble sort. Wiki would be a good place to look. And write a small test program before you incorporate it into your main program (just to ensure you understand how it works).


    And instead of working with several multi-dimensional arrays, I would recommend to create a class which accepts the country and the medals won in the constructor. That way, you won't mess things up if you change the order of the countries but not the medals (as you have it now).

    Regards,
    Jim
    Last edited by jim829; October 25th, 2018 at 09:51 AM.

  3. #3
    Junior Member
    Join Date
    Oct 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to sort the values of a two dimensional array in ascending order.

    I edited my post and I am now determining the descending order of the medal standings. If you are able to, it would be very helpful if you help me determine the ascending order of the medal standings instead.

  4. #4
    Junior Member
    Join Date
    Oct 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to sort the values of a two dimensional array in ascending order.

    Help.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to sort the values of a two dimensional array in ascending order.

    Can you post the program's output to show us what you are talking about?
    Also post an example of correct output.

    Please describe What values are in the metals array?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: December 3rd, 2013, 05:12 PM
  2. Trouble inputting values into a 2-dimensional array
    By thegorila78 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 18th, 2013, 05:37 AM
  3. ascending order array
    By jamie1234 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2013, 06:41 PM
  4. Replies: 3
    Last Post: June 1st, 2011, 12:47 AM
  5. Replies: 4
    Last Post: November 14th, 2010, 11:44 AM