wix-data.WixDataSort

wix-data.WixDataSort

Provides functionality for sorting a query.

Contents

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( )

ascending( )

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

function ascending(...propertyName: String): WixDataQuery

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 alphabetically. The sort is based on the JavaScript sort methodology, which is a lexicographical sort that compares Unicode characters by their code points.
  • 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.

Parameters

propertyName (1 or more) String The properties that will be used in the sort.

Return Value

WixDataQuery A WixDataQuery object representing the refined query.

Examples

Add an ascending sort to a query

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

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

aW1wb3J0IHdpeERhdGEgZnJvbSAnd2l4LWRhdGEnOwoKLy8gLi4uCgp3aXhEYXRhLnF1ZXJ5KCJteUNvbGxlY3Rpb24iKQogIC5hc2NlbmRpbmcoImxhc3RfbmFtZSIsICJmaXJzdF9uYW1lIikKICAuZmluZCgpCiAgLnRoZW4oIChyZXN1bHRzKSA9PiB7CiAgICBsZXQgaXRlbXMgPSByZXN1bHRzLml0ZW1zOwogICAgbGV0IGZpcnN0SXRlbSA9IGl0ZW1zWzBdOwogICAgbGV0IHRvdGFsQ291bnQgPSByZXN1bHRzLnRvdGFsQ291bnQ7CiAgICBsZXQgcGFnZVNpemUgPSByZXN1bHRzLnBhZ2VTaXplOwogICAgbGV0IGN1cnJlbnRQYWdlID0gcmVzdWx0cy5jdXJyZW50UGFnZTsKICAgIGxldCB0b3RhbFBhZ2VzID0gcmVzdWx0cy50b3RhbFBhZ2VzOwogICAgbGV0IGhhc05leHQgPSByZXN1bHRzLmhhc05leHQoKTsKICAgIGxldCBoYXNQcmV2ID0gcmVzdWx0cy5oYXNQcmV2KCk7CiAgICBsZXQgbGVuZ3RoID0gcmVzdWx0cy5sZW5ndGg7CiAgICBsZXQgcXVlcnkgPSByZXN1bHRzLnF1ZXJ5OwogIH0gKQogIC5jYXRjaCggKGVycm9yKSA9PiB7CiAgICBsZXQgZXJyb3JNc2cgPSBlcnJvci5tZXNzYWdlOwogICAgbGV0IGNvZGUgPSBlcnJvci5jb2RlOwogIH0gKTsK
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

aW1wb3J0IHdpeERhdGEgZnJvbSAnd2l4LWRhdGEnOwoKLy8gLi4uCgp3aXhEYXRhLnF1ZXJ5KCJteUNvbGxlY3Rpb24iKQogIC5hc2NlbmRpbmcoImxhc3RfbmFtZSIsICJmaXJzdF9uYW1lIikKICAuZXEoInN0YXR1cyIsICJhY3RpdmUiKQogIC5saW1pdCgxMCkKICAuZmluZCgpCiAgLnRoZW4oIChyZXN1bHRzKSA9PiB7CiAgICBsZXQgaXRlbXMgPSByZXN1bHRzLml0ZW1zOwogICAgbGV0IGZpcnN0SXRlbSA9IGl0ZW1zWzBdOwogICAgbGV0IHRvdGFsQ291bnQgPSByZXN1bHRzLnRvdGFsQ291bnQ7CiAgICBsZXQgcGFnZVNpemUgPSByZXN1bHRzLnBhZ2VTaXplOwogICAgbGV0IGN1cnJlbnRQYWdlID0gcmVzdWx0cy5jdXJyZW50UGFnZTsKICAgIGxldCB0b3RhbFBhZ2VzID0gcmVzdWx0cy50b3RhbFBhZ2VzOwogICAgbGV0IGhhc05leHQgPSByZXN1bHRzLmhhc05leHQoKTsKICAgIGxldCBoYXNQcmV2ID0gcmVzdWx0cy5oYXNQcmV2KCk7CiAgICBsZXQgbGVuZ3RoID0gcmVzdWx0cy5sZW5ndGg7CiAgICBsZXQgcXVlcnkgPSByZXN1bHRzLnF1ZXJ5OwogIH0gKQogIC5jYXRjaCggKGVycm9yKSA9PiB7CiAgICBsZXQgZXJyb3JNc2cgPSBlcnJvci5tZXNzYWdlOwogICAgbGV0IGNvZGUgPSBlcnJvci5jb2RlOwogIH0gKTsK
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( )

descending( )

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

function descending(...propertyName: String): WixDataQuery

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 alphabetically. The sort is based on the JavaScript sort methodology, which is a lexicographical sort that compares Unicode characters by their code points.
  • 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.

Parameters

propertyName (1 or more) String The properties that will be used in the sort.

Return Value

WixDataQuery A WixDataQuery object representing the refined query.

Examples

Add an descending sort to a query

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

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

aW1wb3J0IHdpeERhdGEgZnJvbSAnd2l4LWRhdGEnOwoKLy8gLi4uCgp3aXhEYXRhLnF1ZXJ5KCJteUNvbGxlY3Rpb24iKQogIC5kZXNjZW5kaW5nKCJsYXN0X25hbWUiLCAiZmlyc3RfbmFtZSIpCiAgLmZpbmQoKQogIC50aGVuKCAocmVzdWx0cykgPT4gewogICAgbGV0IGl0ZW1zID0gcmVzdWx0cy5pdGVtczsKICAgIGxldCBmaXJzdEl0ZW0gPSBpdGVtc1swXTsKICAgIGxldCB0b3RhbENvdW50ID0gcmVzdWx0cy50b3RhbENvdW50OwogICAgbGV0IHBhZ2VTaXplID0gcmVzdWx0cy5wYWdlU2l6ZTsKICAgIGxldCBjdXJyZW50UGFnZSA9IHJlc3VsdHMuY3VycmVudFBhZ2U7CiAgICBsZXQgdG90YWxQYWdlcyA9IHJlc3VsdHMudG90YWxQYWdlczsKICAgIGxldCBoYXNOZXh0ID0gcmVzdWx0cy5oYXNOZXh0KCk7CiAgICBsZXQgaGFzUHJldiA9IHJlc3VsdHMuaGFzUHJldigpOwogICAgbGV0IGxlbmd0aCA9IHJlc3VsdHMubGVuZ3RoOwogICAgbGV0IHF1ZXJ5ID0gcmVzdWx0cy5xdWVyeTsKICB9ICkKICAuY2F0Y2goIChlcnJvcikgPT4gewogICAgbGV0IGVycm9yTXNnID0gZXJyb3IubWVzc2FnZTsKICAgIGxldCBjb2RlID0gZXJyb3IuY29kZTsKICB9ICk7Cg==
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

aW1wb3J0IHdpeERhdGEgZnJvbSAnd2l4LWRhdGEnOwoKLy8gLi4uCgp3aXhEYXRhLnF1ZXJ5KCJteUNvbGxlY3Rpb24iKQogIC5kZXNjZW5kaW5nKCJsYXN0X25hbWUiLCAiZmlyc3RfbmFtZSIpCiAgLmVxKCJzdGF0dXMiLCAiYWN0aXZlIikKICAubGltaXQoMTApCiAgLmZpbmQoKQogIC50aGVuKCAocmVzdWx0cykgPT4gewogICAgbGV0IGl0ZW1zID0gcmVzdWx0cy5pdGVtczsKICAgIGxldCBmaXJzdEl0ZW0gPSBpdGVtc1swXTsKICAgIGxldCB0b3RhbENvdW50ID0gcmVzdWx0cy50b3RhbENvdW50OwogICAgbGV0IHBhZ2VTaXplID0gcmVzdWx0cy5wYWdlU2l6ZTsKICAgIGxldCBjdXJyZW50UGFnZSA9IHJlc3VsdHMuY3VycmVudFBhZ2U7CiAgICBsZXQgdG90YWxQYWdlcyA9IHJlc3VsdHMudG90YWxQYWdlczsKICAgIGxldCBoYXNOZXh0ID0gcmVzdWx0cy5oYXNOZXh0KCk7CiAgICBsZXQgaGFzUHJldiA9IHJlc3VsdHMuaGFzUHJldigpOwogICAgbGV0IGxlbmd0aCA9IHJlc3VsdHMubGVuZ3RoOwogICAgbGV0IHF1ZXJ5ID0gcmVzdWx0cy5xdWVyeTsKICB9ICkKICAuY2F0Y2goIChlcnJvcikgPT4gewogICAgbGV0IGVycm9yTXNnID0gZXJyb3IubWVzc2FnZTsKICAgIGxldCBjb2RlID0gZXJyb3IuY29kZTsKICB9ICk7Cg==
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;
  } );