Comparator plus comparator

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

Collections.sort(combo, new LabelComparator()) 

The problem is that it does not sort stressed vowels (my language is Spanish).

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

But if I use collator sorting, I cannot use a comparator (and vice versa). Any ideas?

Thanks in advance!

+6
source share
2 answers

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

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

I wrote a small structure for sorting collections of objects with CollationKeys (and not with Collators):

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

You just need to implement a localizer (or do a POJO to 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 take a look at 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> 
+2
source

Source: https://habr.com/ru/post/902762/


All Articles