Exemplo n.º 1
0
  /**
   * Sorts existing {@link io.realm.RealmResults}.
   *
   * @param fieldName the field name to sort by. Only fields of type boolean, short, int, long,
   *     float, double, Date, and String are supported.
   * @param sortOrder the direction to sort by.
   * @throws java.lang.IllegalArgumentException if field name does not exist.
   */
  public void sort(String fieldName, Sort sortOrder) {
    if (fieldName == null) {
      throw new IllegalArgumentException("fieldName must be provided");
    }
    realm.checkIfValid();
    TableOrView table = getTable();

    if (table instanceof TableView) {
      long columnIndex = getColumnIndex(fieldName);
      ((TableView) table).sort(columnIndex, sortOrder);
    } else {
      throw new IllegalArgumentException(
          "Only RealmResults can be sorted - please use allObject() to create a RealmResults.");
    }
  }
Exemplo n.º 2
0
  /**
   * Sorts existing {@link io.realm.RealmResults}.
   *
   * @param fieldNames an array of field names to sort by. Only fields of type boolean, short, int,
   *     long, float, double, Date, and String are supported.
   * @param sortOrders the directions to sort by.
   * @throws java.lang.IllegalArgumentException if a field name does not exist.
   */
  public void sort(String fieldNames[], Sort sortOrders[]) {
    if (fieldNames == null) {
      throw new IllegalArgumentException("fieldNames must be provided.");
    } else if (sortOrders == null) {
      throw new IllegalArgumentException("sortOrder must be provided.");
    }

    if (fieldNames.length == 1 && sortOrders.length == 1) {
      sort(fieldNames[0], sortOrders[0]);
    } else {
      realm.checkIfValid();
      TableOrView table = getTable();
      if (table instanceof TableView) {
        List<Long> columnIndices = new ArrayList<Long>();
        for (int i = 0; i < fieldNames.length; i++) {
          String fieldName = fieldNames[i];
          long columnIndex = getColumnIndex(fieldName);
          columnIndices.add(columnIndex);
        }
        ((TableView) table).sort(columnIndices, sortOrders);
      }
    }
  }