To sort text data in tables (JTable) according to Vietnamese alphabetical
order, you need a customized Comparator class for Vietnamese language. The following
example adapts from an example in "Sorting and Otherwise Manipulating Data" of
Java Tutorial: How to Use Tables. In
TableSorter.java file, replace the current assignment for
LEXICAL_COMPARATOR with:
Để sắp xếp bảng (JTable) theo thứ tự abc Việt Nam, bạn cần sử dụng một class
Comparator cho Tiếng Việt,
VietComparator.java. Thí dụ sau được dẫn từ thí dụ trong phần
"Sorting and Otherwise Manipulating Data" của trang
Java Tutorial: How to Use Tables. Trong
TableSorter.java file, thay assignment hiện có cho
LEXICAL_COMPARATOR với:
public static final Comparator LEXICAL_COMPARATOR = new VietComparator();
and replace in / và thay trong getComparator(int column) method
if (Comparable.class.isAssignableFrom(columnType))
with/với:
if (columnType != String.class && Comparable.class.isAssignableFrom(columnType))
so that String column type does not use COMPARABLE_COMPARATOR.
để String column type không dùng COMPARABLE_COMPARATOR.
TableSorterDemo.java is
modified with Vietnamese words to demonstrate sorting Tiếng Việt.
TableSorterDemo.java được sửa
đổi với các chữ Việt để thí nghiệm tính năng sắp xếp Tiếng Việt.
LocaleElements_vi.java includes the collation data.Đoạn code trên
sẽ chạy tốt với Java 5.0. Cho Java 1.4.2 trở về trước, bạn cần cung cấp thêm
Vietnamese collation rules.
LocaleElements_vi.java có chứa data đó.

Hình 1: Chưa xếp (unsort).

Hình 2: Xếp cột 'First Name' theo thứ tự từ trên xuống (sort 'First Name' column in ascending order).

Hình 3: Xếp cột 'First Name' theo thứ tự từ dưới lên (sort 'First Name' column in descending order).