Java数组排序UTF-8 [英] Java array sort UTF-8

查看:125
本文介绍了Java数组排序UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对 ArrayList< String> 进行排序,但问题是我的母语字符 - 我的字母是这样的: a,±, b,c,č,d,e,f ... z,ž。如你所见 z 字符从结尾开始是秒,而±是字母表中的第二个,所以在我对数组进行排序之后排序不正确。我的所有母语字符都移动到数组的末尾。示例:

I want to sort an ArrayList<String> but the problem is my native language characters - my alphabet is like this: a, ą, b, c, č, d, e, f ... z, ž. As you see z character is second from the end and ą is second in alphabet, so after I sort my array it is sorted incorrectly. All my native language characters are moved to the end of array. Example:

package lt;

import java.util.ArrayList;
import java.util.Collections;

public class test {
    public static void main(String[] args) {
        List<String> items = new ArrayList<>();
        items.add("bbc");
        items.add("ąbc");
        items.add("abc");
        items.add("zzz");

        System.out.println("Unsorted: ");
        for(String str : items) {
            System.out.println(str);
        }

        Collections.sort(items);
        System.out.println();

        System.out.println("Sorted: ");
        for(String str : items) {
            System.out.println(str);
        }
    }
}

输出:

Unsorted: 
bbc
ąbc
abc
zzz

Sorted: 
abc
bbc
zzz
ąbc

应该是:

Sorted:
abc
ąbc
bbc
zzz


推荐答案

你应该使用 Collat​​or class。

You should use Collator class.

例如

Locale lithuanian = new Locale("lt_LT");
Collator lithuanianCollator = Collator.getInstance(lithuanian);

然后使用此整理器对集合进行排序

And then sort the collection using this collator

Collections.sort(theList, lithuanianCollator);

这篇关于Java数组排序UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆