0

Hey I have this project where the program displays team scores in a descending order but the problem is I can't switch up the team names so they can be in order because the team names array is a string.

 for(int i = 0;i<tabPTS.length;i++){ // tabEquipe is the array containing the team names.
       for (int j = i+1; j<tabPTS.length;j++){
           if(tabPTS[i]<tabPTS[j]){
               temp = tabPTS[i];
               tabPTS[i] = tabPTS[j];
               tabPTS[j] = temp;
           }
       }
   }
     System.out.println("Equipes: ");
    System.out.println();
    System.out.print("\t" + "PJ");
    System.out.print("\t" + "V");
    System.out.print("\t" +  "D");
    System.out.print("\t" +  "PTS");
    System.out.println();
   for(int i = 0;i<tabPTS.length;i++){
        int pos = i;

        System.out.println(tabEquipe[i] + "\t" +   tabPJ[i] + "\t"
                + tabVict[i] + "\t" +  tabDef[i] + "\t" + tabPTS[i]); 

   }

2 Answers2

1

If I understand the question correctly you want to output team names and their scores in sorted order based on their scores. Multiple ways to approach this problem:

1) Try defining a type to hold team name and their score together and then sort an array of such objects based on their score.

2) If you have to keep scores and team names as separate arrays, keep switching the values in the team names array along with the scores array in your selection sort algorithm.

1

To sort a String array in Java, you need to compare each element of the array to all the remaining elements, if the result is greater than 0, swap them.

One solution to do so you need to use two loops (nested) where the inner loop starts with i+1 (where i is the variable of outer loop) to avoid repetitions in comparison.

Here Is the Example

import java.util.Arrays;

public class StringArrayInOrder {
   public static void main(String args[]) {
      String[] myArray = {"JavaFX", "HBase", "OpenCV", "Java", "Hadoop", "Neo4j"};
      int size = myArray.length;

      for(int i = 0; i<size-1; i++) {
         for (int j = i+1; j<myArray.length; j++) {
            if(myArray[i].compareTo(myArray[j])>0) {
               String temp = myArray[i];
               myArray[i] = myArray[j];
               myArray[j] = temp;
            }
         }
      }
      System.out.println(Arrays.toString(myArray));
   }
}

Here is The output of above code

[HBase, Hadoop, Java, JavaFX, Neo4j, OpenCV]

You can also sort an array using the sort() method of the Arrays class.

Like this

String[] myArray = {"JavaFX", "HBase", "OpenCV", "Java", "Hadoop","Neo4j"};
Arrays.sort(myArray);
System.out.println(Arrays.toString(myArray));

You can Sorts arr[] in descending order like this

String arr[] = {"p","q","c"}; 

// Sorts arr[] in ascending order 
        Arrays.sort(arr); 
        System.out.printf("Modified arr[] : \n%s\n\n", 
                          Arrays.toString(arr)); 

        // Sorts arr[] in descending order 
        Arrays.sort(arr, Collections.reverseOrder());

And output

Modified arr[] : 


Modified arr[] : 
[q, p, c]
Kiran Mistry
  • 2,614
  • 3
  • 12
  • 28