Update: the result of the blog below landed here. Thanks to Nathan Freeman, hihi, kudos to me???
In Lotus Script it was not a simple task to sort a document collection. In Java it sort of is.
Idea: use a comparator class, supply with an array of fields upon which the “sorting” should happen and let the Collections.sort do it’s magic:
Comparator class:
public class CollectionComparator implements Comparator<Document> {
String[] sortFields = null;
public CollectionComparator(String[] sortFields) {
this.sortFields = sortFields;
}
public int compare(Document doc1, Document doc2) {
try {
int compared = 0;
// loop all sortFields
for (String field : sortFields) {
Item item1 = doc1.getFirstItem(field);
Item item2 = doc2.getFirstItem(field);
switch (item1.getType()) {
case Item.TEXT:
case Item.AUTHORS:
case Item.NAMES:
case Item.READERS:
String val1 = doc1.getItemValueString(field);
String val2 = doc2.getItemValueString(field);
compared = val1.compareTo(val2);
if (0 != compared) {
return compared;
}
break;
case Item.NUMBERS:
Double d1 = doc1.getItemValueDouble(field);
Double d2 = doc2.getItemValueDouble(field);
compared = d1.compareTo(d2);
if (0 != compared) {
return compared;
}
break;
case Item.DATETIMES:
DateTime dt1 = item1.getDateTimeValue();
DateTime dt2 = item2.getDateTimeValue();
compared = dt2.timeDifference(dt1);
if (0 != compared) {
return compared;
}
break;
}
item1.recycle();
}
return 0;
} catch (NotesException e) {
Logger.logError(e);
}
return 0;
}
}









