Home > Software engineering >  Descening order for the string [name]
Descening order for the string [name]

Time:04-19

help please on sorting string according to the top number/score (btw according to our instructor, we can't use "arrays.sort")

package test;
import java.util.Scanner;

public class Test
{

public static void main(String[] args) {   

 
    
    Scanner sc = new Scanner(System.in);
    String [] name = new String[10];
    int [] score = new int [10];     
    int temp = 0;    
        
    //Displaying elements of original array 
    for (int i = 0; i < 10; i  ) {  
    System.out.println("\nNumber "   (i 1));
    System.out.print("Input your name: ");
        name[i] = sc.next();
    System.out.print("Input your score: ");
        score[i] = sc.nextInt();
    }    
        
    //Sort the array in descending order    
    for (int i = 0; i < score.length; i  ) {     
        for (int j = i 1; j < score.length; j  ) {     
           if(score[i] < score[j]) {    
               temp = score[i];    
               score[i] = score[j];    
               score[j] = temp;    
           }     
        }     
    }    
        
    System.out.println();    
        
    //Displaying elements of array after sorting    
    System.out.println("Elements of array sorted in descending order: ");    
    for (int i = 0; i < 3; i  ) {     
        System.out.println("the score of "   name[i]   " is "  score[i]   ". ");    
    }    
}    

}

The example Input is like this

k1 96
k2 92
k3 94
k4 98
k5 91
k6 95
k7 90
k8 99
k9 93
k10 97

The desired output that I want is like this

k8 99
k4 98
k10 97

But what happens is:

k1 99
k2 98
k3 97

please help, would love a simple explanation. Thank you.

CodePudding user response:

You have two arrays, one named name, and one named score.

You only sort score, while making no changes to name.

I would suggest either one of two solutions:

  1. Keep names and scores in objects, and store these in one array, and sort that array based on scores.

or

  1. Do the same index changes to name as you do to score.

I would suggest trying to implement number 1, as name and score are conceptually part of the same object, like "age" and "name" would be for an object representing a "person".

Hope this helps, good luck with your assignment :-)

CodePudding user response:

//Sort the array in descending order    
    for (int i = 0; i < score.length; i  ) {     
        for (int j = i 1; j < score.length; j  ) {     
           if(score[i] < score[j]) {    
               temp = score[i];    
               score[i] = score[j];    
               score[j] = temp;    
           }     
        }     
    }   

Here, when you sort the arrays, you only swap score elements. To maintain the name/score pairing you should swap even name elements:

//Sort the array in descending order    
    for (int i = 0; i < score.length; i  ) {     
        for (int j = i 1; j < score.length; j  ) {     
           if(score[i] < score[j]) {    
               temp = score[i];    
               score[i] = score[j];    
               score[j] = temp;
               tmp = name[i]
               name[i] = name[j]
               name[j] = tmp
           }     
        }     
    }   
  • Related