sort β GTM Variable Template for Array
sort EXTENDED Array
Returns a sorted array. A custom comparison function can be provided to control the sort order.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβSort strings alphabetically
INPUT
Array To Sort: ['banana', 'apple', 'cherry', 'date']
Add Comparison Function (Optional): false
Compare Function: null
Add Comparison Function (Optional): false
Compare Function: null
OUTPUT
['apple', 'banana', 'cherry', 'date']
Empty array unchanged
INPUT
Array To Sort: []
Add Comparison Function (Optional): false
Compare Function: null
Add Comparison Function (Optional): false
Compare Function: null
OUTPUT
[]
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
sort
Array To Sort
πΎ The array to sort the elements.
Supported formats:
β Array
Supported formats:
β Array
Advanced Setting
βοΈ Optional comparison function to define sort order. If not provided, items are sorted as strings in ascending order.
Supported formats:
β Function
Supported formats:
β Function
Compare Function
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., str => str + ' β¬', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Array To Sort array
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Compare Function (Optional) function
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
sort()
Related Variables
Section titled βRelated VariablesβSame category: Array
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Returns a new array with the elements sorted. Accepts an optional comparison function.** @param {Array} data.src - The array to sort.* @param {boolean} data.add - Whether to use a custom comparison function.* @param {Function} data.cmp - Comparison function to define sort order (used when add is true).* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.** Direct-mode specific parameters:* @param {Function} [data.pre] - Optional pre-processor function to transform src before sorting.** @returns {Array} A new sorted array, or an empty array if input is invalid.** @framework ggLowCodeGTMKit*/const getType = require('getType');
const sort = function(arr, useCustomCompare, compareFn) { if (getType(arr) !== 'array') { return []; } return useCustomCompare && typeof compareFn === 'function' ? arr.slice().sort(compareFn) : arr.slice().sort();};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// sort - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(sort(value, data.add, data.cmp));// ===============================================================================// sort(...) β Apply Mode// ===============================================================================/*return function(value, useCustomCompare, compareFn) { useCustomCompare = data.rp1 ? useCustomCompare : data.add; compareFn = data.rp2 ? compareFn : data.cmp; return out(sort(value, useCustomCompare, compareFn));};*/π§ͺ View Test Scenarios (5 tests)
β
Array of numbers - should sort numerically as strings by defaultβ
'[example] Sort strings alphabetically'β
Array with custom comparison function - should sort in descending orderβ
'[example] Empty array unchanged'β
Non-array input - should return empty array