sort (array)
  • 23 May 2023
  • Dark
    Light
  • PDF

sort (array)

  • Dark
    Light
  • PDF

Article summary

sort (array) Task Purpose

The sort task sorts the elements of a given array and returns the sorted array. The default sort order is ascending (e.g., from A to Z or from 0 to 9). This task method changes the position of the elements in the original array.

Potential Use Case

The sort task can be used to sort arrays of numbers, strings and objects. You could use this task to arrange a list of names in alphabetical order, or a collection of dates in numerical order. For any outcome data that needs to be captured and sorted, you could potentially use sort .

Properties

Input and output properties are shown below.

Input Type Description
arr Array Required. The array to be sorted.
key String Optional. For objects, a value used to set the sort order.
limit Number Optional. The number of records to return.
reverse Boolean Required. If true, the arr is sorted in reverse (descending) order. Default is false (ascending order).


Output Type Description
response Array The output of sorting the arr.

Example 1

In this IAP example:

  • An array of incoming reference variables for arr are statically set:
    [{"name": "cat"},{"name": "dog"},{"name": "hamster"},{"name": "ant"},{"name": "Lion"}].
    Notice the name "Lion" is uppercase.

  • The key parameter is name, and the record limit is 4.

  • The reverse parameter is false (default).

    sortArray-01

  • The sort order will be ascending in the response, with "Lion" listed first in the sorted array since it begins with a capital (uppercase) letter.

    sortArray-02

Example 2

In this IAP example:

  • An array of incoming variables for arr are statically set. The reference variables are:
    [{"car": "Audi"},{"car": "Volvo"},{"car": "Honda"},{"car": "BMW"}]. Notice all the initial letters in the names are uppercase.

  • The key parameter is car, and the limit is left blank.

  • The reverse parameter is false (default).

    sortArray-03

  • The resulting sort order will be alphabetical (ascending order).

    sortArray-04

Example 3

In this IAP example:

  • An array of incoming variables for arr are statically set. The reference variables are:
    ["ant","bee","Cat","dog","Elephant"]. Notice the mix of lowercase and uppercase values.

  • The key and limit are left blank.

  • The reverse parameter is true.

    sortArray-05

  • The resulting sort order will be descending, with all the lowercase values listed in reverse alphabetical order followed by the uppercase values.

    sortArray-06


Was this article helpful?

What's Next
Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.