简体   繁体   中英

Sorting like scores in parallel arrays alphabetically

For an assignment I am required to ask for a number of students. Prompt the user to enter a lastname for a student and a score and then sort the scores in descending order and display it. I have done all that but cannot figure out how to sort name alphabetically if they have the same score.

example input:

brian 11

john 33

phil 22

joe 22

adam 33


my output:

john 33

adam 33

phil 22

joe 22

brian 11


output I need:

adam 33

john 33

joe 22

phil 22

brian 11


I cannot get the desired output. I usually can solve these kinds of problems, but this one is tearing me apart.. please help!

import java.util.*;

public class className
{
    public static void main(String[] args)
    {
        //Scanner input for # of students
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of students: ");
        int numofstudents = input.nextInt();
        System.out.println();

        //Initializes an array of strings to the size of the # of students
        String[] names = new String[numofstudents];

        //Initializes an array of ints to the size of the # of students
        int[] array = new int[numofstudents];

        //For loop which asks for each students name and score until # of students reached
        for(int i = 0; i < numofstudents; i++)
        {
            System.out.print("Enter the student's lastname: ");
            names[i] = input.next();
            System.out.print("Enter the student's score: ");
            array[i] = input.nextInt();
            System.out.println();
        }
        //Sorts the arrays for names and scores of students
        selectionSort(names, array);
        //Output
        System.out.println();
        System.out.println();
        System.out.println("Number of Students: " + numofstudents);
        //System.out.println(Arrays.toString(names));
        for(int i = 0; i < numofstudents; i++)
        {
            System.out.println(names[i] + ": " + array[i]);
        }
    }

    public static void selectionSort(String[] names, int[] array) {
        for(int i = array.length - 1; i >= 1; i--) {
            String temp;
            int currentMax = array[0];
            int currentMaxIndex = 0;
            for(int j = 1; j <= i; j++) {
                if (currentMax > array[j]) {
                    currentMax = array[j];
                    currentMaxIndex = j;
                }
            }
                if (currentMaxIndex != i) {
                    temp = names[currentMaxIndex];
                    names[currentMaxIndex] = names[i];
                    names[i] = temp;
                    array[currentMaxIndex] = array[i];
                    array[i] = currentMax;
                }
        }
    }
}

There are actually two Sorting to do. Start sorting your arrays alphabetically no matter what the score is. Later, sort the arrays according to score and you get your result.

Edit: While sorting according to score, you should stop when t[i]=t[i-1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM