There is an array containing number and name data as follows:
Number Name
10 Brycen
6 Devan
3 Rylan
1 Gordy
2 Tim
4 Curtis
5 Abe
9 Melvin
8 Ansel
7 Dalton
The output should be:
Number Name
1 Gordy
2 Tim
3 Rylan
4 Curtis
5 Abe
6 Devan
7 Dalton
8 Ansel
9 Melvin
10 Brycen
I've tried it using an Array of Objects and FOR..END-FOR looping. System.out.println(data[i].number); displays output in order but for System.out.println(data[i].name); display the original state.
class StudentList { //DaftarSiswa
public int number;
public String name;
public StudentList(int number, String name) {
this.number = number;
this.name = name;
}
}
class OrderNumber { //NomorUrut
public static void main(String[] args) {
StudentList[] data = new StudentList[10];
data[0] = new StudentList(10, "Brycen");
data[1] = new StudentList( 6, "Devan");
data[2] = new StudentList( 3, "Rylan");
data[3] = new StudentList( 1, "Gordy");
data[4] = new StudentList( 2, "Tim");
data[5] = new StudentList( 4, "Curtis");
data[6] = new StudentList( 5, "Abe");
data[7] = new StudentList( 9, "Melvin");
data[8] = new StudentList( 8, "Ansel");
data[9] = new StudentList( 7, "Dalton");
for (int i = 0; i < data.length; i ) {
for (int j = i 1; j < data.length; j ) {
int tmp = 0;
if (data[i].number > data[j].number) {
tmp = data[i].number;
data[i].number = data[j].number;
data[j].number = tmp;
}
}
System.out.println(data[i].name);
}
}
}
CodePudding user response:
You're just swapping the numbers. You need to swap the entire object.:
if (data[i].number > data[j].number) {
Student tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
CodePudding user response:
You can make your Student
(renamed from StudentList
) class to implement Comparable interface in order to sort your array using Arrays.sort method. In this case you must add to Student class implementation of compareTo method.
class Student implements Comparable<Student> { //DaftarSiswa
public int number;
public String name;
public Student(int number, String name) {
this.number = number;
this.name = name;
}
@Override
public int compareTo(Student o) {
return this.number - o.number;
}
@Override
public String toString() {
return "Student {number=" number ", name='" name "'}\n";
}
}
public class Test { //NomorUrut
public static void main(String[] args) {
Student[] data = new Student[10];
data[0] = new Student(10, "Brycen");
data[1] = new Student(6, "Devan");
data[2] = new Student(3, "Rylan");
data[3] = new Student(1, "Gordy");
data[4] = new Student(2, "Tim");
data[5] = new Student(4, "Curtis");
data[6] = new Student(5, "Abe");
data[7] = new Student(9, "Melvin");
data[8] = new Student(8, "Ansel");
data[9] = new Student(7, "Dalton");
Arrays.sort(data);
System.out.println(Arrays.toString(data));
}
}
CodePudding user response:
You can use Java Streams for that, which a more elegant than loops, in my opinion.
Assuming StudentList
class normally would have getters (and setters), you could it do this way:
public class OrderNumber { //NomorUrut
public static void main(String[] args) {
StudentList[] data = new StudentList[10];
data[0] = new StudentList(10, "Brycen");
data[1] = new StudentList( 6, "Devan");
data[2] = new StudentList( 3, "Rylan");
data[3] = new StudentList( 1, "Gordy");
data[4] = new StudentList( 2, "Tim");
data[5] = new StudentList( 4, "Curtis");
data[6] = new StudentList( 5, "Abe");
data[7] = new StudentList( 9, "Melvin");
data[8] = new StudentList( 8, "Ansel");
data[9] = new StudentList( 7, "Dalton");
Arrays.stream(data) // Start stream
.sorted(Comparator.comparingInt(StudentList::getNumber)) // Sort by Number
.forEach(student -> System.out.println(student.getName())); // Output (like you shown in code)
}
}
If you also want to have an list or array sorted in that order you could collect
it.
// get sorted list of students
var students = Arrays.stream(data) // Start stream
.sorted(Comparator.comparingInt(StudentList::getNumber)) // Sort by Number
.collect(Collectors.toList()); // collect to list
// if you really want an array
var studentsArray = students.toArray(new StudentList[students.size()]);