Search HoldMyTwist

Browse all posts

Sort tasks by numeric priority in Make

The exercise shows using Make Free to sort three made-up task records by numeric priority (maintaining full records). It checks the type of priority field. It compares number values with number strings in quotes.

A brass sorting arm arranging navy blocks from shortest to tallest on aqua steps

The three dummy tasks have number priority 10, 2, 1. The sorting on priority field should preserve priority for each task. Numerically the order would be 1, 2, 10. (but text value order may be different).

Before you start

You need a Make Free account (opens in a new tab).

If you already have one, just log in.

The JSON and Tools modules do not need a new app connection.

And the scenario is still Inactive.

This is a Run once exercise, so it is done manually.

1. Create three task records

  1. In Scenarios, click Create scenario.
  2. In the top left, give it the name Numeric priority practice.
  3. Click JSON > Parse JSON.
  4. Don't fill in anything for Data structure.
  5. Paste the given fake data into JSON string.
  6. Save your module and scenario. If you get the unused output warning, click Save anyway.
  7. Click Run once. If you get the warning again, click Run anyway.
  8. Before adding and mapping your next module, check your JSON output. This is the first run that will show you the structure of your JSON.
Text to copy

{"jobs":[{"task":"Write","priority":10},{"task":"Fix","priority":2},{"task":"Review","priority":1}]}

Full Make editor showing task records with unquoted numeric priorities 10, 2 and 1

Full Make editor showing task records with unquoted numeric priorities 10, 2 and 1
Open full-size image

The fixture itself references 10, 2, 1 (no quotes). So lower numbers have higher priority for this exercise. But the order of priority might differ for another task list, so pick the sorting direction you want.

2. Sort the array by its priority field

  1. Hide JSON results (Close JSON results panel)
  2. Click on plus sign next to JSON. Insert Tools > Set variable
  3. Name your variable sorted_jobs
  4. Give it a value. Take your sort expression as provided. And use (coloured) mapping token jobs from JSON module 1. If your JSON module number is different, select jobs array (from mapping panel) of your JSON module.
  5. Save module and scenario.
Text to copy

{{sort(1.jobs; "asc"; "priority")}}

Sort all the records by their priority property.

Full Make editor applying sort to jobs in ascending order using priority

Full Make editor applying sort to jobs in ascending order using priority

Sort all the records by their priority property.

Open full-size image

Watch out

For the same top 3, the numeric ascending order is 1, 2, 10. The quoted strings of numbers are 1, 10, 2 when sorted. Be sure to check the type of source before you sort it.

3. Check every sorted record

  1. Pick 'Run once'.
  2. Click on the result bubble for 'Tools'. Expand 'Output' then 'sorted_jobs'. Now expand all three entries.
  3. Note the order of task and priority:

Review => 1

Fix => 2

Write => 10

Full Make editor showing Review priority 1, Fix priority 2 and Write priority 10

Full Make editor showing Review priority 1, Fix priority 2 and Write priority 10
Open full-size image

The output will be a bundle, which contains the whole sorted array. Each task is associated with its original priority.

If 10 appears before 2

JSON uses double-quotes to enclose text values. Numbers are not enclosed in quotes. You will need to convert the text numbers from another app into a number before sorting on a numeric basis. Double-check the sorting is as expected based on values you know. There is a parseNumber (opens in a new tab) function for Make that will convert the number string into a number. For this exercise, asc is ascending, and desc is the reverse. Maybe use a spreadsheet for a one-time sort? Don't forget Free-plan limits if you're planning on Make.

Sources: Make array functions (opens in a new tab); Make JSON documentation (opens in a new tab). Screenshots show fictional practice data.

Share this post