用于UTF8字母的JAVA比较器

iJa*_*ava 5 java comparator

我查了几篇关于比较器的帖子,但我坚持了一点.

我正在使用的比较器:

@Override
    public int compare(BaseFolder arg0, BaseFolder arg1) {
        try {
            Object value1 = arg0.getName();
            Object value2 = arg1.getName();

            Collator lithuanianCollator = Collator.getInstance(new Locale("lt_LT"));
            lithuanianCollator.setStrength(Collator.PRIMARY);
            int value = lithuanianCollator.compare(value1.toString(), value2.toString());

            return SortOrder.ASCENDING.equals(sortOrder) ? value : -1 * value;
        }
        catch(Exception e) {
            throw new RuntimeException();
        }
    }
Run Code Online (Sandbox Code Playgroud)

它确实排序,但它在立陶宛字母上不能正常工作,我不明白为什么.

编辑:由于某种原因,似乎排序取决于字符串长度.

例如.

在此输入图像描述

编辑:

public class BaseFolder {
    private String id;
    private String name;
    private String description;
    private String lastModifiedBy;
    private String lastModificationDate;
    private String createdBy;
    private String creationDate;
    private String parentId;

    public BaseFolder() {
    }
    public BaseFolder(CmisObject obj) {
        this.id = obj.getId();
        this.name = obj.getName();
        this.description = obj.getDescription();
        this.lastModificationDate = DateFormatUtils.format(obj.getLastModificationDate().getTime(), "yyyy-MM-dd");
        this.lastModifiedBy = obj.getLastModifiedBy();
        this.createdBy = obj.getCreatedBy();
        this.creationDate = DateFormatUtils.format(obj.getCreationDate().getTime(), "yyyy-MM-dd");


    }
    public BaseFolder(String id, String name, String description, String parentId) {
        super();
        this.id = id;
        this.name = name;
        this.description = description;
        this.parentId = parentId;
    }

    public Map<String, Object> getProperties() {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(PropertyIds.PARENT_ID, "cmis:parentId");
        properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
        properties.put(PropertyIds.NAME, getName());
        properties.put(PropertyIds.DESCRIPTION, getDescription());
        return properties;
    }
Run Code Online (Sandbox Code Playgroud)

使用java 8,Primefaces,JSF

reo*_*eos 2

我尝试使用此代码来订购

\n\n
public static void main(String[] args) {\n\n    String[] words = {"\xc4\x85", "a", "\xc4\xaf", "i", "\xc4\x85\xc4\x85\xc4\x85\xc4\x85\xc4\x85", "aaaaa"};\n\n    Collator en_USCollator = Collator.getInstance(new Locale("en","US"));\n    Collator lt_LTCollator = Collator.getInstance(new Locale("lt","LT"));\n\n    sortStrings(en_USCollator, words);\n    System.out.println(Arrays.toString(words));\n    sortStrings(lt_LTCollator, words);\n    System.out.println(Arrays.toString(words));\n}\n\npublic static void sortStrings(Collator collator, String[] words) {\n    String tmp;\n    for (int i = 0; i < words.length; i++) {\n        for (int j = i + 1; j < words.length; j++) { \n            if (collator.compare(words[i], words[j]) > 0) {\n                tmp = words[i];\n                words[i] = words[j];\n                words[j] = tmp;\n            }\n        }\n    }       \n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是输出

\n\n
[a, \xc4\x85, aaaaa, \xc4\x85\xc4\x85\xc4\x85\xc4\x85\xc4\x85, i, \xc4\xaf]\n[a, \xc4\x85, aaaaa, \xc4\x85\xc4\x85\xc4\x85\xc4\x85\xc4\x85, i, \xc4\xaf]\n
Run Code Online (Sandbox Code Playgroud)\n\n

更新

\n\n

您可以使用 RuleBasedCollat​​or

\n\n
    String simple = "< a< \xc4\x85< i< \xc4\xaf";\n    RuleBasedCollator lt_LTCollator = new RuleBasedCollator(simple);\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是输出

\n\n
[a, aaaaa, \xc4\x85, \xc4\x85\xc4\x85\xc4\x85\xc4\x85\xc4\x85, i, \xc4\xaf]\n
Run Code Online (Sandbox Code Playgroud)\n\n

这里有更多信息\n http://docs.oracle.com/javase/7/docs/api/java/text/RuleBasedCollat ​​or.html

\n