# Sort Index


# Description

A <SortIndex> tag is used to define an index by which the user of your search engine can sort their results.

Each SortIndex must be defined as a child tag within a <SortIndexes> tag.

Multiple indexes can be specified within one SortIndex. The second index is used as a second sorting mechanism when the search result contains items that have equal values of the first index listed.

1search also provides a random sort functionality. See Random Sort to learn more.

When calling a sort to be performed, even if you reverse the order of the sort, the null items will always automatically be placed at the bottom of the list.

# Syntax

<SortIndexes>
    <SortIndex Name="string">
        <Index Key="string" Direction="string"/>
        <Index Key="string" Direction="string"/>
    </SortIndex>
</SortIndexes>

# Attributes

Note: attributes marked with * are required.

Attributes of the <SortIndex> tag include:

  • Name* (string) :
    • The name of the SortIndex. This value must be unique across all SortIndexes defined.

Attributes of the <Index> tag include:

  • Key* (string) :
  • Func* (string) Replaces Key:
    • Key can be replaced with a Func if a plain JSON path is not enough.
  • Direction* ("Ascending" or "Descending) :
    • Determines whether items are sorted using the specified index from smallest to largest or largest to smallest.

# Example One

Assume you have an implementation of 1search for a collection of book items listed for sale. You have a "Price" range index & you would like users to be able to sort search results by the lowest or highest price first.

# Code

To implement the scenario above, the following XML would be needed to create your price range index:

<Ranges>
    <Range Name = "Price" DataType = "System.Double" Key = "Specification.Price" />
</Ranges>

Then, to implement the "Price" range as a SortIndex, the following XML would be required:

<SortIndexes>
    <SortIndex Name = "PriceLowToHigh" >
        <Index Key = "Specification.Price" Direction = "Ascending" />
    </SortIndex>
    <SortIndex Name = "PriceHighToLow" >
        <Index Key = "Specification.Price" Direction = "Descending" />
    </SortIndex>
</SortIndexes>