0

i have code like this and i need specified output like i mentioned in bottom, sorry if my explanation isn't clear but here is my code:

public static int[] bubbleAscending(int x[], String y[], int z[]) {//sort by score
    for (int i = 0; i < (z.length-1); i++) {
        for (int j = 0; j < (z.length-i-1); j++) {
            if (z[j] > z[j + 1]) {
                int temp = z[j];
                z[j] = z[j + 1];
                z[j + 1] = temp;
            }
        }
    }
    return z;
}

public static void showArray(int[] x, String[] y, int[] z) {
    for(int i= 0; i < x.length; i++) {
        System.out.println(x[i]+"   "+y[i]+"    "+z[i]);
    }
    System.out.println("");
}

public static void main(String[] args) {
    int number[] = {23,43,54,67,76};
    String name[] = {"Angeline","Brian","Catherine","Daniel","Elliot"};
    int score[] = {89,56,46,93,67};
    System.out.println("********************");
    System.out.println("       DATA    ");
    System.out.println("********************");

    System.out.println(".:. Score Data : ");
    System.out.println("------------------------------");
    System.out.println("NUMBER  Name    Score");
    System.out.println("------------------------------");
    showArray(number,name,score);
    
    nilai1 = bubbleAscending(number,name,score);
    System.out.println("Data After Sorting");
    System.out.println("********************");
    System.out.println("       DATA    ");
    System.out.println("********************");

    System.out.println(".:. Score Data : ");
    System.out.println("------------------------------");
    System.out.println("NUMBER  Name    Score");
    System.out.println("------------------------------");
    showArray(number,name,score);
}

i expected this output:

NUMBER  Name        Score //got right sorting for score
54      Catherine   46
43      Brian       56
76      Elliot      67
23      Angeline    89
67      Daniel      93 

but my output:

NUMBER  Name        Score
23      Angeline    46
43      Brian       56
54      Catherine   67
67      Daniel      89
76      Elliot      93

now my question how to sort the number and name according to score? thanks

2 Answers2

1

Sort the x and y array as well.

You know based on the output under 'Score' that your sorting algorithm works as expected, so that's not the problem. However, if you look at the inner section:

if (z[j] > z[j + 1]) {
    int temp = z[j];
    z[j] = z[j + 1];
    z[j + 1] = temp;
}

You're only modifying the z-array and not the other two.

Addendum: The solution given by Rohan Kumar is the cleaner and more flexible solution. However, I'm pretty sure this is either a homework question or some coding challenge about sorting multiple arrays at once.

0

I think it would be better if you combine all there three number, name and score fields into a class:

private static class Student {
    int number;
    String name;
    int score;

    public Student(int number, String name, int score) {
        this.number = number;
        this.name = name;
        this.score = score;
    }
}

This way you won't have to deal with trouble of maintaining three different arrays, you can create a single Student array which would have all these attributes:

Student[] students = new Student[] {
        new Student(23, "Angeline", 89),
        new Student(43, "Brian", 56),
        new Student(54, "Catherine", 46),
        new Student(67, "Daniel", 93),
        new Student(76, "Elliot", 67)
};

This would also make your sorting a lot easier and flexible.

Sort by Score:

Arrays.sort(students, Comparator.comparingInt(o -> o.score));

Sort by Name:

Arrays.sort(students, Comparator.comparing(o -> o.name));

Sort by Number:

Arrays.sort(students, Comparator.comparingInt(o -> o.number));
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40