private static boolean sort()

in tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/SortingUtils.java [93:181]


  private static boolean sort(
      final FacesContext facesContext, final AbstractUISheet sheet, final Object data, final UIColumn column,
      final boolean ascending, final SheetState sheetState, Comparator comparator) {

    final Comparator actualComparator;

    if (data instanceof List || data instanceof Object[]) {
      try {
        final UIComponent child = getFirstSortableChild(column.getChildren());
        if (child != null) {
          final String attribute = (child instanceof AbstractUICommand ? Attributes.label : Attributes.value).getName();
          final ValueExpression expression = child.getValueExpression(attribute);
          if (expression != null) {
            final String var = sheet.getVar();
            if (var == null) {
              LOG.error("No sorting performed. Property var of sheet is not set!");
              addNotSortableMessage(facesContext, column);
              return false;
            }
            actualComparator = new ValueExpressionComparator(facesContext, var, expression, !ascending, comparator);
          } else {
            LOG.error("No sorting performed, because no expression found for "
                    + "attribute '{}' in component '{}' with id='{}'! You may check the type of the component!",
                attribute, child.getClass().getName(), child.getClientId());
            addNotSortableMessage(facesContext, column);
            return false;
          }
        } else {
          LOG.error("No sorting performed. Value is not instanceof List or Object[]!");
          addNotSortableMessage(facesContext, column);
          return false;
        }
      } catch (final Exception e) {
        return false;
      }

      // memorize selected rows
      List<Object> selectedDataRows = null;
      if (sheetState.getSelectedRows().size() > 0) {
        selectedDataRows = new ArrayList<>(sheetState.getSelectedRows().size());
        Object dataRow;
        for (final Integer index : sheetState.getSelectedRows()) {
          if (data instanceof List) {
            dataRow = ((List) data).get(index);
          } else {
            dataRow = ((Object[]) data)[index];
          }
          selectedDataRows.add(dataRow);
        }
      }

      // do sorting
      if (data instanceof List) {
        Collections.sort((List) data, actualComparator);
      } else { // value is instanceof Object[]
        Arrays.sort((Object[]) data, actualComparator);
      }

      // restore selected rows
      if (selectedDataRows != null) {
        sheetState.getSelectedRows().clear();
        for (final Object dataRow : selectedDataRows) {
          int index = -1;
          if (data instanceof List) {
            for (int i = 0; i < ((List) data).size() && index < 0; i++) {
              if (dataRow == ((List) data).get(i)) {
                index = i;
              }
            }
          } else {
            for (int i = 0; i < ((Object[]) data).length && index < 0; i++) {
              if (dataRow == ((Object[]) data)[i]) {
                index = i;
              }
            }
          }
          if (index >= 0) {
            sheetState.getSelectedRows().add(index);
          }
        }
      }

    } else {  // DataModel?, ResultSet, Result or Object
      LOG.warn("Sorting not supported for type '{}'.", data != null ? data.getClass().toString() : "null");
      addNotSortableMessage(facesContext, column);
      return false;
    }
    return true;
  }