CodeAPI

WixDataSort

Provides functionality for sorting a query.

Table of Contents

FUNCTIONS

?
Perform actions on an object.
ascending( )Adds a sort to a query or sort, sorting by the specified properties in ascending order.
descending( )Adds a sort to a query or sort, sorting by the specified properties in descending order.

ascending( )

Adds a sort to a query or sort, sorting by the specified properties in ascending order.

Description

The ascending() function refines a WixDataQuery or WixDataSort to sort in ascending order of the specified properties. If you specify more than one property, ascending() sorts the results in ascending order by each property in the order they are listed.

You can sort the following types:

  • Number: Sorts numerically.
  • Date: Sorts by date and time.
  • String: Sorts lexicographically, so "abc" comes after "XYZ".
  • Reference: Compares by the ID of the referenced item as a String.

If a property contains a number as a String, that value will be sorted alphabetically and not numerically. Items that do not have a value for the specified sort property are ranked lowest.

Syntax

function ascending(propertyName: string): WixDataQuery
PARAMETERS
?
The kind of data the property stores.
propertyName
string
The properties used in the sort.
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
A WixDataQuery object representing the refined query.

Examples

Add an ascending sort to a query

let newQuery = query.ascending("last_name", "first_name");

Create a query, add an ascending sort, and run it

import wixData from 'wix-data';

// ...

wixData.query("myCollection")
  .ascending("last_name", "first_name")
  .find()
  .then( (results) => {
    let items = results.items;
    let firstItem = items[0];
    let totalCount = results.totalCount;
    let pageSize = results.pageSize;
    let currentPage = results.currentPage;
    let totalPages = results.totalPages;
    let hasNext = results.hasNext();
    let hasPrev = results.hasPrev();
    let length = results.length;
    let query = results.query;
  } )
  .catch( (error) => {
    let errorMsg = error.message;
    let code = error.code;
  } );

Create a query, add an ascending sort, and run it

import wixData from 'wix-data';

// ...

wixData.query("myCollection")
  .ascending("last_name", "first_name")
  .eq("status", "active")
  .limit(10)
  .find()
  .then( (results) => {
    let items = results.items;
    let firstItem = items[0];
    let totalCount = results.totalCount;
    let pageSize = results.pageSize;
    let currentPage = results.currentPage;
    let totalPages = results.totalPages;
    let hasNext = results.hasNext();
    let hasPrev = results.hasPrev();
    let length = results.length;
    let query = results.query;
  } )
  .catch( (error) => {
    let errorMsg = error.message;
    let code = error.code;
  } );

descending( )

Adds a sort to a query or sort, sorting by the specified properties in descending order.

Description

The descending() function refines a WixDataQuery or WixDataSort to sort in descending order of the specified properties. If you specify more than one property, descending() sorts the results in descending order by each property in the order they are listed.

You can sort the following types:

  • Number: Sorts numerically.
  • Date: Sorts by date and time.
  • String: Sorts lexicographically, so "abc" comes before "XYZ".
  • Reference: Compares by the ID of the referenced item as a String.

If a property contains a number as a String, that value will be sorted alphabetically and not numerically. Items that do not have a value for the specified sort property are ranked lowest.

Syntax

function descending(propertyName: string): WixDataQuery
PARAMETERS
?
The kind of data the property stores.
propertyName
string
The properties used in the sort.
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
A WixDataQuery object representing the refined query.

Examples

Add an descending sort to a query

let newQuery = query.descending("last_name", "first_name");

Create a query, add an descending sort, and run it

import wixData from 'wix-data';

// ...

wixData.query("myCollection")
  .descending("last_name", "first_name")
  .find()
  .then( (results) => {
    let items = results.items;
    let firstItem = items[0];
    let totalCount = results.totalCount;
    let pageSize = results.pageSize;
    let currentPage = results.currentPage;
    let totalPages = results.totalPages;
    let hasNext = results.hasNext();
    let hasPrev = results.hasPrev();
    let length = results.length;
    let query = results.query;
  } )
  .catch( (error) => {
    let errorMsg = error.message;
    let code = error.code;
  } );

Create a query, add an descending sort, and run it

import wixData from 'wix-data';

// ...

wixData.query("myCollection")
  .descending("last_name", "first_name")
  .eq("status", "active")
  .limit(10)
  .find()
  .then( (results) => {
    let items = results.items;
    let firstItem = items[0];
    let totalCount = results.totalCount;
    let pageSize = results.pageSize;
    let currentPage = results.currentPage;
    let totalPages = results.totalPages;
    let hasNext = results.hasNext();
    let hasPrev = results.hasPrev();
    let length = results.length;
    let query = results.query;
  } )
  .catch( (error) => {
    let errorMsg = error.message;
    let code = error.code;
  } );