Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
96 lines (75 sloc) 2.19 KB

Sorting

Sort results that are returned from the API by using the sort parameter with the field name you wish to sort against.

{% hint style="info" %} To reverse the sort order, prepend the field with a minus. {% endhint %}

Currently, sorting is supported for the following endpoints, and against the following fields:

Endpoint Fields to sort against
Brands created_at, description, name, slug, status, updated_at
Categories created_at, description, name, slug, status, updated_at
Collections created_at, description, name, slug, status, updated_at
Products commodity_type, created_at, description, manage_stock, name, sku ,slug, status, updated_at
Product Variations name
Orders payment, shipping, status

The example below shows how to make a request to get all products, and sort them by the created_at timestamp.

Sort products by created_at ASC

{% tabs %} {% tab title="cURL" %}

curl -X GET https://api.moltin.com/v2/products?sort=created_at
     -H "Authorization: Bearer XXXX"

{% endtab %}

{% tab title="JavaScript SDK" %}

const MoltinGateway = require('@moltin/sdk').gateway

const Moltin = MoltinGateway({
  client_id: 'X'
})

Moltin.Products.All()
  .Sort('created_at')
  .then(products => {
    // Do something
  })

{% endtab %}

{% tab title="Swift SDK" %}

let moltin = Moltin(withClientID: "<your client ID>")

moltin.product.sort("created_at").all {
  // Do something
}

{% endtab %} {% endtabs %}

Sort products by created_at DESC

{% tabs %} {% tab title="cURL" %}

curl -X GET https://api.moltin.com/v2/products?sort=-created_at
     -H "Authorization: Bearer XXXX"

{% endtab %}

{% tab title="JavaScript SDK" %}

const MoltinGateway = require('@moltin/sdk').gateway

const Moltin = MoltinGateway({
  client_id: 'X'
})

Moltin.Products.All()
  .Sort('-created_at')
  .then(products => {
    // Do something
  })

{% endtab %}

{% tab title="Swift SDK" %}

let moltin = Moltin(withClientID: "<your client ID>")

moltin.product.sort("-created_at").all {
  // Do something
}

{% endtab %} {% endtabs %}