Search in sources :

Example 1 with ExceptionComparator

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;
}
Also used : PageException(lucee.runtime.exp.PageException) ArrayImpl(lucee.runtime.type.ArrayImpl) ExpressionException(lucee.runtime.exp.ExpressionException) SortRegister(lucee.runtime.type.comparator.SortRegister) ExceptionComparator(lucee.runtime.type.comparator.ExceptionComparator) Array(lucee.runtime.type.Array) SortRegisterComparator(lucee.runtime.type.comparator.SortRegisterComparator) NumberSortRegisterComparator(lucee.runtime.type.comparator.NumberSortRegisterComparator) NumberSortRegisterComparator(lucee.runtime.type.comparator.NumberSortRegisterComparator)

Aggregations

ExpressionException (lucee.runtime.exp.ExpressionException)1 PageException (lucee.runtime.exp.PageException)1 Array (lucee.runtime.type.Array)1 ArrayImpl (lucee.runtime.type.ArrayImpl)1 ExceptionComparator (lucee.runtime.type.comparator.ExceptionComparator)1 NumberSortRegisterComparator (lucee.runtime.type.comparator.NumberSortRegisterComparator)1 SortRegister (lucee.runtime.type.comparator.SortRegister)1 SortRegisterComparator (lucee.runtime.type.comparator.SortRegisterComparator)1