use of lucee.runtime.type.comparator.ExceptionComparator in project Lucee by lucee.
the class StructSort method call.
public static Array call(PageContext pc, Struct base, String sortType, String sortOrder, String pathToSubElement) throws PageException {
boolean isAsc = true;
PageException ee = null;
if (sortOrder.equalsIgnoreCase("asc"))
isAsc = true;
else if (sortOrder.equalsIgnoreCase("desc"))
isAsc = false;
else
throw new ExpressionException("invalid sort order type [" + sortOrder + "], sort order types are [asc and desc]");
Collection.Key[] keys = CollectionUtil.keys(base);
SortRegister[] arr = new SortRegister[keys.length];
boolean hasSubDef = pathToSubElement != null;
for (int i = 0; i < keys.length; i++) {
Object value = base.get(keys[i], null);
if (hasSubDef) {
value = VariableInterpreter.getVariable(pc, Caster.toCollection(value), pathToSubElement);
}
arr[i] = new SortRegister(i, value);
}
ExceptionComparator comp = null;
// text
if (sortType.equalsIgnoreCase("text"))
comp = new SortRegisterComparator(pc, isAsc, false, true);
else // text no case
if (sortType.equalsIgnoreCase("textnocase"))
comp = new SortRegisterComparator(pc, isAsc, true, true);
else // numeric
if (sortType.equalsIgnoreCase("numeric"))
comp = new NumberSortRegisterComparator(isAsc);
else {
throw new ExpressionException("invalid sort type [" + sortType + "], sort types are [text, textNoCase, numeric]");
}
Arrays.sort(arr, 0, arr.length, comp);
ee = comp.getPageException();
if (ee != null) {
throw ee;
}
Array rtn = new ArrayImpl();
for (int i = 0; i < arr.length; i++) {
rtn.append(keys[arr[i].getOldPosition()].getString());
}
return rtn;
}
Aggregations