【问题标题】:JAVA comparator for UTF8 lettersUTF8 字母的 JAVA 比较器
【发布时间】:2015-12-09 15:49:46
【问题描述】:

我查看了几篇关于比较器的帖子,但我一直停留在某一点上。

我正在使用的比较器:

@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();
        }
    }

它可以排序,但它不能在立陶宛字母上正常工作,我不知道为什么。

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

例如。

编辑:

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;
    }

使用 java 8、Primefaces、JSF

【问题讨论】:

  • 我猜因为 value1value2 是 Object 类型,所以 toString() 返回对象的字符串表示形式。可以分享BaseFolder的代码吗?
  • 排序有什么问题?
  • 已编辑:基本文件夹类。关于排序。首先应该是 a,然后是 ą ,或者 i 在 į 之前
  • 尝试将Object value1 = arg0.getName(); 更改为String value1 = arg0.getName();Object value2 = arg1.getName(); 更改为String value2 = arg1.getName();
  • 可悲的是,不,没有区别,仍然产生相同的输出。

标签: java comparator


【解决方案1】:

我试过这个代码来订购

public static void main(String[] args) {

    String[] words = {"ą", "a", "į", "i", "ąąąąą", "aaaaa"};

    Collator en_USCollator = Collator.getInstance(new Locale("en","US"));
    Collator lt_LTCollator = Collator.getInstance(new Locale("lt","LT"));

    sortStrings(en_USCollator, words);
    System.out.println(Arrays.toString(words));
    sortStrings(lt_LTCollator, words);
    System.out.println(Arrays.toString(words));
}

public static void sortStrings(Collator collator, String[] words) {
    String tmp;
    for (int i = 0; i < words.length; i++) {
        for (int j = i + 1; j < words.length; j++) { 
            if (collator.compare(words[i], words[j]) > 0) {
                tmp = words[i];
                words[i] = words[j];
                words[j] = tmp;
            }
        }
    }       
}

这是输出

[a, ą, aaaaa, ąąąąą, i, į]
[a, ą, aaaaa, ąąąąą, i, į]

更新

你可以使用 RuleBasedCollat​​or

    String simple = "< a< ą< i< į";
    RuleBasedCollator lt_LTCollator = new RuleBasedCollator(simple);

这是输出

[a, aaaaa, ą, ąąąąą, i, į]

这里有更多信息 http://docs.oracle.com/javase/7/docs/api/java/text/RuleBasedCollator.html

【讨论】:

  • 和这里一样,应该是 [a, aaaaa, ą, ąąąąąą, i, į ] 编辑:即使 [aaaaa, a, ąąąąą, ą, į, i ] 现在也可以,如果这有什么不同
  • 如果对您有帮助,请告诉我。
  • 优胜者鸡肉晚餐! ! ! ;) 工作正常,谢谢先生,帮了我很多!
猜你喜欢
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-07
相关资源
最近更新 更多