I've checked several posts about comparators, but I stuck at one point.
Comparator I' am using:
@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();
}
}
It does sort, but it doesn't work properly on Lithuanian letters, and I got no idea why.
EDIT: seems sorting depends on string length, for some reason.
For example.
EDIT:
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;
}
Using java 8, Primefaces, JSF
I tried this code to order
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;
}
}
}
}
This is the output
[a, ą, aaaaa, ąąąąą, i, į]
[a, ą, aaaaa, ąąąąą, i, į]
UPDATE
You can use RuleBasedCollator
String simple = "< a< ą< i< į";
RuleBasedCollator lt_LTCollator = new RuleBasedCollator(simple);
This is the output
[a, aaaaa, ą, ąąąąą, i, į]
Here there is more information http://docs.oracle.com/javase/7/docs/api/java/text/RuleBasedCollator.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With