Search in sources :

Example 1 with SortRegister

use of lucee.runtime.type.comparator.SortRegister in project Lucee by lucee.

the class QueryImpl method sort.

private void sort(QueryColumn column, int order) throws PageException {
    int type = column.getType();
    SortRegister[] arr = ArrayUtil.toSortRegisterArray(column);
    Arrays.sort(arr, (type == Types.BIGINT || type == Types.BIT || type == Types.INTEGER || type == Types.SMALLINT || type == Types.TINYINT || type == Types.DECIMAL || type == Types.DOUBLE || type == Types.NUMERIC || type == Types.REAL) ? (Comparator) new NumberSortRegisterComparator(order == ORDER_ASC) : (Comparator) new SortRegisterComparator(null, order == ORDER_ASC, true, true));
    for (int i = 0; i < columns.length; i++) {
        column = columns[i];
        int len = column.size();
        QueryColumnImpl newCol = new QueryColumnImpl(this, columnNames[i], columns[i].getType(), len);
        for (int y = 1; y <= len; y++) {
            newCol.set(y, column.get(arr[y - 1].getOldPosition() + 1, null));
        }
        columns[i] = newCol;
    }
}
Also used : SortRegister(lucee.runtime.type.comparator.SortRegister) SortRegisterComparator(lucee.runtime.type.comparator.SortRegisterComparator) NumberSortRegisterComparator(lucee.runtime.type.comparator.NumberSortRegisterComparator) SortRegisterComparator(lucee.runtime.type.comparator.SortRegisterComparator) NumberSortRegisterComparator(lucee.runtime.type.comparator.NumberSortRegisterComparator) Comparator(java.util.Comparator) NumberSortRegisterComparator(lucee.runtime.type.comparator.NumberSortRegisterComparator)

Example 2 with SortRegister

use of lucee.runtime.type.comparator.SortRegister 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)

Example 3 with SortRegister

use of lucee.runtime.type.comparator.SortRegister in project Lucee by lucee.

the class ArrayUtil method toSortRegisterArray.

/**
 * @param column
 * @return array
 */
public static SortRegister[] toSortRegisterArray(QueryColumn column) {
    SortRegister[] arr = new SortRegister[column.size()];
    int type = column.getType();
    for (int i = 0; i < arr.length; i++) {
        arr[i] = new SortRegister(i, toSortRegisterArray(column.get(i + 1, null), type));
    }
    return arr;
}
Also used : SortRegister(lucee.runtime.type.comparator.SortRegister)

Aggregations

SortRegister (lucee.runtime.type.comparator.SortRegister)3 NumberSortRegisterComparator (lucee.runtime.type.comparator.NumberSortRegisterComparator)2 SortRegisterComparator (lucee.runtime.type.comparator.SortRegisterComparator)2 Comparator (java.util.Comparator)1 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