Sort clauses for tables (Java)

Many Data API commands use sort clauses to sort rows by column values or to perform vector search.

Sort by column values

Use a sort clause to sort rows by column values in ascending or descending order.

When sorting by multiple columns, the order of the columns in the sort clause controls the order of the sorting.

For best performance, only sort on indexed columns, partition keys, and clustering keys. Otherwise, the Data API can perform in-memory sorting, which can have performance implications.

import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.cursor.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;

public class Example {

  public static void main(String[] args) {
    // Get an existing table
    Table<Row> table =
        DataAPIClients.clientHCD("USERNAME", "PASSWORD")
            .getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
            .getTable("TABLE_NAME");

    // Find rows
    Filter filter = Filters.eq("is_checked_out", false);
    TableFindOptions options =
        new TableFindOptions().sort(Sort.ascending("rating"), Sort.descending("title"));
    TableFindCursor<Row, Row> cursor = table.find(filter, options);
    // Iterate over the found rows
    for (Row row : cursor) {
      System.out.println(row);
    }
  }
}

To find the rows with a vector column value that is most similar to a given vector, use a sort clause with the vector embeddings that you want to match. For more information, see Find data with vector search.

The vector column must be indexed.

If your table has multiple vector columns, you can only sort on one vector column at a time.

Vector search returns up to 1000 rows per search operation, unless you specify a lower limit.

Example sorting against a search vector

import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.vector.DataAPIVector;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.cursor.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;

public class Example {

  public static void main(String[] args) {
    // Get an existing table
    Table<Row> table =
        DataAPIClients.clientHCD("USERNAME", "PASSWORD")
            .getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
            .getTable("TABLE_NAME");

    // Find rows
    TableFindOptions options =
        new TableFindOptions()
            .sort(
                Sort.vector(
                    "summary_genres_vector",
                    new DataAPIVector(new float[] {0.08f, -0.62f, 0.39f})));
    TableFindCursor<Row, Row> cursor = table.find(options);
    // Iterate over the found rows
    for (Row row : cursor) {
      System.out.println(row);
    }
  }
}

Sort by lexicographical matching (BM25-based ranking)

Lexicographical matching is currently in public preview. Development is ongoing, and the features and functionality are subject to change. Hyper-Converged Database (HCD), and the use of such, is subject to the DataStax Preview Terms.

To find rows with a text or ascii column value that is most relevant to a given string of space-separated keywords or terms, sort on a column that is associated with a text index.

Lexicographical matching is only available for text or ascii columns that have a text index, not a regular index. For more information, see Create a text index (Java) and Indexes in tables (Java).

When performing this type of sort, you can’t include additional sort clauses.

import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOneOptions;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Optional;

public class Example {

  public static void main(String[] args) {
    // Get an existing table
    Table<Row> table =
        DataAPIClients.clientHCD("USERNAME", "PASSWORD")
            .getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
            .getTable("TABLE_NAME");

    // Find a row
    TableFindOneOptions options =
        new TableFindOneOptions().sort(Sort.lexical("summary", "futuristic laboratory discovery"));

    Optional<Row> result = table.findOne(options);
    System.out.println(result);
  }
}

Commands that support sort clauses for tables

There are many Data API commands that support sort clauses for tables.

For more examples of sort clauses, see the reference for the command that you want to run:

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM