-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSortInJava.java
More file actions
48 lines (38 loc) · 1.47 KB
/
Copy pathSortInJava.java
File metadata and controls
48 lines (38 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Arrays;
import java.util.Comparator;
public class SortInJava {
public static void main(String[] args) {
//sắp xếp mảng số nguyên
int [] nums = {3,6,1,6,2,7,4};
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));
//Sắp xếp mảng chuỗi
String [] str = {"Java", "HTML", "Ruby", "CSS", "C++"};
Arrays.sort(str);
System.out.println(Arrays.toString(str));
Student []students = new Student[5];
students[0] = new Student("KKK", 21);
students[1] = new Student("EEE", 27);
students[2] = new Student("CCC", 25);
students[3] = new Student("AAA", 19);
students[4] = new Student("HHH", 24);
//Sử dụng interface Comparable sắp xếp theo tên
Arrays.sort(students);
System.out.println("Danh sách sắp xếp theo tên: ");
for (Student student : students) {
System.out.println(student);
}
//Sử dụng comparator để sắp xếp theo tuôir
Arrays.sort(students, new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o1.getAge() - o2.getAge();
}
});
System.out.println("Danh sách sắp xếp theo tuổi: ");
for (Student student : students) {
System.out.println(student);
}
}
}