整理器加比较器

发布于 2024-12-18 19:50:17 字数 425 浏览 1 评论 0原文

我有一个 beans OptionItemDTO 的集合(属性标签和值),我目前正在使用类似的东西按标签排序:

Collections.sort(combo, new LabelComparator())

问题是它不会对重音元音进行排序(我的语言环境是西班牙语)。

根据此文档 http://blogs.oracle.com/CoreJavaTechTips/entry/sorting_strings我必须使用 Collat​​or 类。

但是,如果我使用带有整理器的排序,则无法使用比较器(反之亦然)。有什么想法吗?

提前致谢!

I have a collection of beans OptionItemDTO (properties label and value) and I'm currently using something like this to sort by label:

Collections.sort(combo, new LabelComparator())

The problem is that it does not sort stressed vowels (my locale is spanish).

According to this document http://blogs.oracle.com/CoreJavaTechTips/entry/sorting_strings I must use a Collator class.

But if I use the sort with collator, I can't use the comparator (and vice-versa). Any ideas?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜唯美灬不弃 2024-12-25 19:50:17

您的 LabelComparator 应委托给 Collat​​or 来比较 DTO 的标签:

public int compare(OptionItemDTO dto1, OptionItemDTO dto2) {
    return collator.compare(dto1.getLabel(), dto2.getLabel());
}

Your LabelComparator should delegate to a Collator to compare the labels of your DTOs:

public int compare(OptionItemDTO dto1, OptionItemDTO dto2) {
    return collator.compare(dto1.getLabel(), dto2.getLabel());
}
混吃等死 2024-12-25 19:50:17

用于使用 Collat​​ionKeys(而不是 Collat​​ors)对对象集合进行排序:

我编写了一个小型框架, html" rel="nofollow">http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/text/Localizeds.html

你只需要实现一个 Localizer (或者使您的 POJO 实现 Localized)以提供字符串表示形式:

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/text/Localizer.html

http://softsmithy.sourceforge.net/lib/docs /api/org/softsmithy/lib/text/Localizable.html

您可以查看一些示例的单元测试:

该库是开源的。

https://sourceforge.net/projects/softsmithy/files/softsmithy/v0.1 1/

<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>lib-core</artifactId>  
    <version>0.1</version>  
</dependency>  

I have written a small framework to sort collections of objects with CollationKeys (rather than Collators):

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/text/Localizables.html

You just have to implement a Localizer (or make your POJO implement Localizable) to provide a string representation:

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/text/Localizer.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/text/Localizable.html

You can have a look at the unit tests for some samples:

http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/lib/main-golden/file/5c4db802573b/lib-core/src/test/java/org/softsmithy/lib/text/LocalizablesTest.java

The library is open source.

https://sourceforge.net/projects/softsmithy/files/softsmithy/v0.1/

<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>lib-core</artifactId>  
    <version>0.1</version>  
</dependency>  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文