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:
- Convert your array into a “safe sortable” structure (usually adding a sortable key)
- Use
sort()(or a workaround if your tenant/region doesn’t expose it yet) - 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
- 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 }]
- Add a Compose (or Set variable) with this expression:
sort(variables('MyArray'), 'createdDate')
That returns the array sorted ascending (oldest → newest).
- 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
- Build a new array where you add a normalized ISO date field (ex:
createdDateIso) - Sort by that field
- (Optionally) remove helper field afterward
Steps
- Initialize an empty array variable:
- Initialize variable →
Normalized(Array) =[]
- Apply to each item in
variables('MyArray'), append toNormalized:
- 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
createdDateis a string like18/02/2026 11:36, you may needconvertTimeZone()or manual parsing. The key is: turn it into yyyy-MM-ddTHH:mm:ssZ first.
- Sort:
sort(variables('Normalized'), 'createdDateIso')
- 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
- Initialize variable
Latest(Object) ={} - Initialize variable
LatestDate(String) =0001-01-01T00:00:00Z - Apply to each item:
- If
items('Apply_to_each')?['createdDate']is greater thanvariables('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
| Goal | Recommended Expression / Pattern | Works when |
|---|---|---|
| Sort oldest → newest | sort(variables('MyArray'),'createdDate') | createdDate is ISO 8601 text |
| Sort newest → oldest | reverse(sort(variables('MyArray'),'createdDate')) | createdDate is ISO 8601 text |
| Date is not ISO | Normalize to yyyy-MM-ddTHH:mm:ssZ, then sort | Date is localized / inconsistent |
| Only need newest item | Track max date in an Apply to each | Large arrays / performance concern |
