Sort (Lucene 10.5.1 core API)
Description: declaration: package: org.apache.lucene.search, class: Sort

Package org.apache.lucene.search

# Class Sort

java.lang.Object
org.apache.lucene.search.Sort

public final class Sort extends Object
Encapsulates sort criteria for returned hits.

A `Sort` can be created with an empty constructor, yielding an object that will instruct searches to return their hits sorted by relevance; or it can be created with one or more `SortField`s.

Since:
  lucene 1.4
See Also:
  - `SortField`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final Sort`
  `INDEXORDER`
  Represents sorting by index order.
  `static final Sort`
  `RELEVANCE`
  Represents sorting by computed relevance.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Sort()`
  Sorts by computed relevance.
  `Sort(SortField... fields)`
  Sets the sort to the given criteria in succession: the first SortField is checked first, but if it produces a tie, then the second SortField is used to break the tie, etc.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `equals(Object o)`
  Returns true if `o` is equal to this.
  `SortField[]`
  `getSort()`
  Representation of the sort criteria.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `boolean`
  `needsScores()`
  Returns true if the relevance score is needed to sort documents.
  `Sort`
  `rewrite(IndexSearcher searcher)`
  Rewrites the SortFields in this Sort, returning a new Sort if any of the fields changes during their rewriting.
  `String`
  `toString()`

  ### Methods inherited from class java.lang.Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- ## Field Details

  - ### RELEVANCE

    public static final Sort RELEVANCE
    Represents sorting by computed relevance. Using this sort criteria returns the same results as calling `IndexSearcher#search()`without a sort criteria, only with slightly more overhead.

  - ### INDEXORDER

    public static final Sort INDEXORDER
    Represents sorting by index order.

- ## Constructor Details

  - ### Sort

    public Sort()
    Sorts by computed relevance. This is the same sort criteria as calling `IndexSearcher#search()`without a sort criteria, only with slightly more overhead.

  - ### Sort

    public Sort(SortField... fields)
    Sets the sort to the given criteria in succession: the first SortField is checked first, but if it produces a tie, then the second SortField is used to break the tie, etc. Finally, if there is still a tie after all SortFields are checked, the internal Lucene docid is used to break it.

- ## Method Details

  - ### getSort

    public SortField[] getSort()
    Representation of the sort criteria.
    Returns:
      Array of SortField objects used in this sort criteria

  - ### rewrite

    public Sort rewrite(IndexSearcher searcher) throws IOException
    Rewrites the SortFields in this Sort, returning a new Sort if any of the fields changes during their rewriting.
    Parameters:
      `searcher` - IndexSearcher to use in the rewriting
    Returns:
      `this` if the Sort/Fields have not changed, or a new Sort if there is a change
    Throws:
      `IOException` - Can be thrown by the rewriting
    WARNING: This API is experimental and might change in incompatible ways in the next release.

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`

  - ### equals

    public boolean equals(Object o)
    Returns true if `o` is equal to this.
    Overrides:
      `equals` in class `Object`

  - ### hashCode

    public int hashCode()
    Returns a hash code value for this object.
    Overrides:
      `hashCode` in class `Object`

  - ### needsScores

    public boolean needsScores()
    Returns true if the relevance score is needed to sort documents.