Example #1
0
  private static String _sort(Object[] arr, String sortType, String sortOrder, String delimiter)
      throws ExpressionException {

    // check sortorder
    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]");

    // text
    if (sortType.equalsIgnoreCase("text")) {
      TextComparator comp = new TextComparator(isAsc, false);
      Arrays.sort(arr, comp);
      ee = comp.getPageException();
    }
    // text no case
    else if (sortType.equalsIgnoreCase("textnocase")) {
      TextComparator comp = new TextComparator(isAsc, true);
      Arrays.sort(arr, comp);
      ee = comp.getPageException();
    }
    // numeric
    else if (sortType.equalsIgnoreCase("numeric")) {
      NumberComparator comp = new NumberComparator(isAsc);
      Arrays.sort(arr, comp);
      ee = comp.getPageException();

    } else {
      throw new ExpressionException(
          "invalid sort type [" + sortType + "], sort types are [text, textNoCase, numeric]");
    }
    if (ee != null) {
      throw new ExpressionException("invalid value to sort the list", ee.getMessage());
    }
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < arr.length; i++) {
      if (i != 0) sb.append(delimiter);
      sb.append(arr[i]);
    }
    return sb.toString();
  }
Example #2
0
  /**
   * sort values of a array
   *
   * @param sortType search type (text,textnocase,numeric)
   * @param sortOrder (asc,desc)
   * @throws PageException
   */
  public synchronized void sort(String sortType, String sortOrder) throws PageException {
    if (getDimension() > 1)
      throw new ExpressionException("only 1 dimensional arrays can be sorted");

    // check sortorder
    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]");

    // text
    if (sortType.equalsIgnoreCase("text")) {
      TextComparator comp = new TextComparator(isAsc, false);
      // Collections.sort(list,comp);
      Arrays.sort(arr, offset, offset + size, comp);
      ee = comp.getPageException();
    }
    // text no case
    else if (sortType.equalsIgnoreCase("textnocase")) {
      TextComparator comp = new TextComparator(isAsc, true);
      // Collections.sort(list,comp);
      Arrays.sort(arr, offset, offset + size, comp);
      ee = comp.getPageException();

    }
    // numeric
    else if (sortType.equalsIgnoreCase("numeric")) {
      NumberComparator comp = new NumberComparator(isAsc);
      // Collections.sort(list,comp);
      Arrays.sort(arr, offset, offset + size, comp);
      ee = comp.getPageException();

    } else {
      throw new ExpressionException(
          "invalid sort type [" + sortType + "], sort types are [text, textNoCase, numeric]");
    }
    if (ee != null) {
      throw new ExpressionException("can only sort arrays with simple values", ee.getMessage());
    }
  }