Chúng ta có thể sắp xếp các phần tử của:
- Các đối tượng String.
- Các đối tượng của lớp Wrapper.
- Các đối tượng của lớp do người dùng định nghĩa (User-defined).
Lớp Collections trong java cung cấp các phương thức static để sắp xếp các phần tử của collection. Nếu các phần tử collection thuộc kiểu Set hoặc Map, chúng ta có thể sử dụng TreeSet hoặc TreeMap. Nhưng chúng ta không thể sắp xếp các phần tử của List. Collections cung cấp phương thức sort() để phân sắp xếp các phần tử của List.

Phương thức Collections.sort(List list)
public void sort(List list): được sử dụng để sắp xếp các phần tử của List. Với điều kiện các phần tử của List phải là kiểu Comparable. Nghĩa là lớp các phần tử phải được implements giao diện Comparable
Note: Lớp String và các lớp Wrapper được implements giao diện Comparable. Vì vậy nếu bạn lưu trữ các đối tượng của lớp String và Wrapper thì chúng đã là kiểu Comparable. Khi đó bạn có thể áp dụng phương thức Collections.sort(List list) mà không phải cài đặt gì thêm.
Còn đối với List của các đối tượng do người dùng tự định nghĩa thì bạn phải implements giao diện Comparable cho lớp của đối tượng đó.
Các ví dụ về sorting trong Collections
Dưới đây là các ví dụ về sorting trong Collections với các đối tượng String, đối tượng của Wrapper, và đối tượng của lớp do người dùng định nghĩa.
Ví dụ sắp xếp đối tượng String
Ví dụ 1: Sử dụng phương thức Collections.sort(list) để sắp xếp các phần tử String của list theo thứ tự tăng dần.
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
|
package vn.viettuts.collection;
/**
* CollectionsExample3 class
*
* @author viettuts.vn
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsExample3 {
public static void main(String args[]) {
// create list
List<String> list = new ArrayList<String>();
// add elements to list
list.add( "Java" );
list.add( "C" );
list.add( "C++" );
list.add( "PHP" );
list.add( "Python" );
// sort list
Collections.sort(list);
// show list
for (String element : list) {
System.out.println(element);
}
}
}
|
Kết quả:
C
C++
Java
PHP
Python
Đối với String implements giao diện Comparable và mặc định là sắp xếp tăng dần. Vậy thì để sắp xếp giảm dần thì bản phải làm gì? Hãy xem ví dụ dưới đây nhé.
Ví dụ 1: Sử dụng phương thức Collections.sort(list, new Comparator<T>()) để sắp xếp các phần tử String của list theo thứ tự giảm dần hoặc tăng dần.
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
|
package vn.viettuts.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* CollectionsExample4 class
*
* @author viettuts.vn
*/
public class CollectionsExample4 {
public static void main(String args[]) {
// create list
List<String> list = new ArrayList<String>();
// add elements to list
list.add( "Java" );
list.add( "C" );
list.add( "C++" );
list.add( "PHP" );
list.add( "Python" );
// sort list
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
// show list
for (String element : list) {
System.out.println(element);
}
}
}
|
Kết quả:
Python
PHP
Java
C++
C
Trong ví dụ trên bạn phải tạo ra một đối tượng nặc danh của lớp Comparator để cài đặt tiêu chí so sánh trong phương thức phương thức compare() để so sánh 2 chuỗi String với nhau.
Ví dụ sắp xếp đối tượng của lớp Wrapper
Dưới đây là ví dụ sắp xếp các đối tượng Double tăng dần (mặc định) và giảm dần (sử dụng giao tiếp Comparator).
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
|
package vn.viettuts.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* CollectionsExample5 class
*
* @author viettuts.vn
*/
public class CollectionsExample5 {
public static void main(String[] args) {
// create list
List<Double> list = new ArrayList<Double>();
// add elements to list
list.add(15D);
list.add(20D);
list.add( 16 .5D);
// sort list ASC
Collections.sort(list);
// show list
System.out.println( "show list ASC:" );
for (Double element : list) {
System.out.println(element);
}
// sort list DESC
Collections.sort(list, new Comparator<Double>() {
@Override
public int compare(Double o1, Double o2) {
return o1 > o2 ? - 1 : 1 ;
}
});
// show list
System.out.println( "show list DESC:" );
for (Double element : list) {
System.out.println(element);
}
}
}
|
Kết quả:
show list ASC:
15.0
16.5
20.0
show list DESC:
20.0
16.5
15.0
Ví dụ sắp xếp đối tượng của lớp Student (User-defined)
Cách 1: lớp Student implements giao diện java.lang.Comparable để cài đặt phương thức compareTo(). Bạn cần phải cài đặt tiêu chí để so sánh trong phương thức compareTo() để so sánh các đối tượng Student với nhau, dưới đây là ví dụ sắp xếp các đối tượng Student tăng dần theo name:
Tạo lớp Student.java
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
|
package vn.viettuts.collection;
/**
* Student class
*
* @author viettuts.vn
*/
class Student implements Comparable<Student> {
private int id;
private String name;
private int age;
private String address;
public Student() {
}
public Student( int id, String name, int age, String address) {
super ();
this .id = id;
this .name = name;
this .age = age;
this .address = address;
}
// getter & setter
@Override
public String toString() {
return "Student@id=" + id + ",name=" + name
+ ",age=" + age + ",address=" + address;
}
@Override
public int compareTo(Student student) {
// sort student's name by ASC
return this .getName().compareTo(student.getName());
}
}
|
Tạo lớp CollectionsExample6.java
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
|
package vn.viettuts.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* CollectionsExample6 class
*
* @author viettuts.vn
*/
public class CollectionsExample6 {
public static void main(String[] args) {
// create list students
List<Student> listStudents = new ArrayList<Student>();
// add students to list
listStudents.add( new Student( 1 , "Vinh" , 19 , "Hanoi" ));
listStudents.add( new Student( 2 , "Hoa" , 19 , "Hanoi" ));
listStudents.add( new Student( 3 , "Phu" , 20 , "Hanoi" ));
listStudents.add( new Student( 4 , "Quy" , 22 , "Hanoi" ));
// sort list student
Collections.sort(listStudents);
// show list students
for (Student student : listStudents) {
System.out.println(student.toString());
}
}
}
|
Kết quả:
Student@id=2,name=Hoa,age=19,address=Hanoi
Student@id=3,name=Phu,age=20,address=Hanoi
Student@id=4,name=Quy,age=22,address=Hanoi
Student@id=1,name=Vinh,age=19,address=Hanoi
Cách 2: Tạo đối tượng nặc danh java.util.Comparator để cài đặt phương thức compare(). Bạn cần phải cài đặt tiêu chí để so sánh trong phương thức compare() để so sánh các đối tượng Student với nhau, dưới đây là ví dụ sắp xếp các đối tượng Student tăng dần theo name:
Tạo lớp Student.java
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
|
package vn.viettuts.collection;
/**
* Student class
*
* @author viettuts.vn
*/
class Student {
private int id;
private String name;
private int age;
private String address;
public Student() {
}
public Student( int id, String name, int age, String address) {
super ();
this .id = id;
this .name = name;
this .age = age;
this .address = address;
}
// getter & setter
@Override
public String toString() {
return "Student@id=" + id + ",name=" + name
+ ",age=" + age + ",address=" + address;
}
}
|
Tạo lớp CollectionsExample7.java
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
|
package vn.viettuts.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* CollectionsExample7 class
*
* @author viettuts.vn
*/
public class CollectionsExample7 {
public static void main(String[] args) {
// create list students
List<Student> listStudents = new ArrayList<Student>();
// add students to list
listStudents.add( new Student( 1 , "Vinh" , 19 , "Hanoi" ));
listStudents.add( new Student( 2 , "Hoa" , 19 , "Hanoi" ));
listStudents.add( new Student( 3 , "Phu" , 20 , "Hanoi" ));
listStudents.add( new Student( 4 , "Quy" , 22 , "Hanoi" ));
// sort list student
Collections.sort(listStudents, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
}
});
// show list students
for (Student student : listStudents) {
System.out.println(student.toString());
}
}
}
|
Kết quả:
Student@id=2,name=Hoa,age=19,address=Hanoi
Student@id=3,name=Phu,age=20,address=Hanoi
Student@id=4,name=Quy,age=22,address=Hanoi
Student@id=1,name=Vinh,age=19,address=Hanoi
Nguồn tin: viettuts