Array sort by struct key

Supported in: Batch, Streaming

Returns a sorted array of the given input array of structs sorted by the values of the given struct keys.

Expression categories: Array

Declared arguments

  • Input array - Input array of structs to be sorted.
    Expression<Array<Struct>>
  • Sort keys - Struct keys to sort array by in order of sort priority. Sort nested struct elements with multiple entries like ['author', 'age'].
    List<Tuple<StructLocator, Enum<Ascending, Descending>>>

Output type: Array<Struct>

Examples

Example 1: Base case

Argument values:

  • Input array: [ {
    age: 20,
    }, {
    age: 10,
    }, {
    age: 30,
    } ]
  • Sort keys: [(age, ASCENDING)]

Output: [ {
age: 10,
}, {
age: 20,
}, {
age: 30,
} ]


Example 2: Base case

Argument values:

  • Input array: [ {
    age: 20,
    }, {
    age: 10,
    }, {
    age: 30,
    } ]
  • Sort keys: [(age, DESCENDING)]

Output: [ {
age: 30,
}, {
age: 20,
}, {
age: 10,
} ]


Example 3: Base case

Argument values:

  • Input array: [ {
    age: 20,
    height: 77,
    }, {
    age: 20,
    height...
  • Sort keys: [(age, ASCENDING), (height, DESCENDING)]

Output: [ {
age: 10,
height: 80,
}, {
age: 10,
height...


Example 4: Base case

Argument values:

  • Input array: [ {
    age: 20,
    height: 77,
    }, {
    age: 20,
    height...
  • Sort keys: [(age, ASCENDING), (height, ASCENDING)]

Output: [ {
age: 10,
height: 65,
}, {
age: 10,
height...


Example 5: Base case

Argument values:

  • Input array: [ {
    subStructKey: {
    age: 20,
    },
    }, {
    subStructKey: {
    age: 10,
    },
    }, {
    subStructKey: {
    age: 30,
    },
    } ]
  • Sort keys: [(subStructKey.age, ASCENDING)]

Output: [ {
subStructKey: {
age: 10,
},
}, {
subStructKey: {
age: 20,
},
}, {
subStructKey: {
age: 30,
},
} ]


Example 6: Null case

Argument values:

  • Input array: [ {
    age: 10,
    }, {
    age: null,
    }, {
    age: 30,
    } ]
  • Sort keys: [(age, ASCENDING)]

Output: [ {
age: null,
}, {
age: 10,
}, {
age: 30,
} ]


Example 7: Null case

Argument values:

  • Input array: [ {
    age: 10,
    }, {
    age: null,
    }, {
    age: 30,
    } ]
  • Sort keys: [(age, DESCENDING)]

Output: [ {
age: 30,
}, {
age: 10,
}, {
age: null,
} ]