Sorting an Array Variable by a Date Field in Power Automate (Step-by-step)

In Power Automate, an array variable doesn’t have a native “Sort” action. The standard pattern is:

  1. Convert your array into a “safe sortable” structure (usually adding a sortable key)
  2. Use sort() (or a workaround if your tenant/region doesn’t expose it yet)
  3. Pick the first/last item or rebuild the array in the right order

Below are the most reliable approaches (with copy/paste expressions).


Approach A (Best): sort() on an ISO date string (most common)

If your date field is already ISO 8601 like:
2026-02-18T14:36:45.283Z

…then sorting is easy because string order == chronological order.

Steps

  1. Ensure your array variable is something like:
[
{ "createdDate": "2026-02-18T14:36:45.283Z", "id": 1 },
{ "createdDate": "2026-02-20T09:10:00.000Z", "id": 2 }
]
  1. Add a Compose (or Set variable) with this expression:
sort(variables('MyArray'), 'createdDate')

That returns the array sorted ascending (oldest → newest).

  1. If you want descending (newest → oldest), reverse it:
reverse(sort(variables('MyArray'), 'createdDate'))

✅ This is the cleanest solution when the date is ISO text.


Approach B: When the date is not ISO (e.g., “18/02/2026 11:36”)

If your date is “localized”, you must normalize it first, otherwise string sort will be wrong.

Strategy

  1. Build a new array where you add a normalized ISO date field (ex: createdDateIso)
  2. Sort by that field
  3. (Optionally) remove helper field afterward

Steps

  1. Initialize an empty array variable:
  • Initialize variableNormalized (Array) = []
  1. Apply to each item in variables('MyArray'), append to Normalized:
  • Append to array variable with a JSON object like:
json(concat(
'{',
'"createdDateIso":"', formatDateTime(items('Apply_to_each')?['createdDate'], 'yyyy-MM-ddTHH:mm:ssZ'), '",',
'"original":', string(items('Apply_to_each')),
'}'
))

If your createdDate is a string like 18/02/2026 11:36, you may need convertTimeZone() or manual parsing. The key is: turn it into yyyy-MM-ddTHH:mm:ssZ first.

  1. Sort:
sort(variables('Normalized'), 'createdDateIso')
  1. For descending:
reverse(sort(variables('Normalized'), 'createdDateIso'))

Approach C: “Top 1 newest” without sorting everything (faster for big arrays)

If your goal is just “get the latest item”, you can reduce.

Steps

  1. Initialize variable Latest (Object) = {}
  2. Initialize variable LatestDate (String) = 0001-01-01T00:00:00Z
  3. Apply to each item:
  • If items('Apply_to_each')?['createdDate'] is greater than variables('LatestDate'), update both.

Condition expression:

greater(items('Apply_to_each')?['createdDate'], variables('LatestDate'))

Then set:

  • LatestDate = items('Apply_to_each')?['createdDate']
  • Latest = items('Apply_to_each')

✅ Best when you only need 1 item and the array is large.


MS Learn references (official)

  • Power Automate expression functions overview (includes array functions patterns):
  • sort() / array manipulation patterns in expressions documentation:

(If you want, I can pull the exact MS Learn pages for sort() availability and examples based on your tenant behavior.)


Final “Cheat Sheet” (copy/paste)

Ascending by date

sort(variables('MyArray'), 'createdDate')

Descending by date

reverse(sort(variables('MyArray'), 'createdDate'))

Summary Table

GoalRecommended Expression / PatternWorks when
Sort oldest → newestsort(variables('MyArray'),'createdDate')createdDate is ISO 8601 text
Sort newest → oldestreverse(sort(variables('MyArray'),'createdDate'))createdDate is ISO 8601 text
Date is not ISONormalize to yyyy-MM-ddTHH:mm:ssZ, then sortDate is localized / inconsistent
Only need newest itemTrack max date in an Apply to eachLarge arrays / performance concern

Edvaldo Guimrães Filho Avatar

Published by