JMAC Docs

JMAC / docs / DynSort

DynSort()Method : Array.prototype

Sorts an array of objects by user-provided property key


Values

function DynSort(type: string, invert?: boolean): Array

Parameters

type
A string containing the name of the nested property key to sort by. Must be present in every nested object.
Defaults to the first property key of the first nested object. [experimental]
invert
If set to true, function will return a reverse sort.
Defaults to false.

Returns

Array
A new Array mutated by either a logical or inverted DynSort.

Common Errors

TypeError: Parameter 'type' must be assigned a value
Usually occurs when type is passed a non-string or undefined.
Error: Parameter 'type' does not match any key in this
Occurs when type is not a valid property key in any child of the Array calling the method.

Example


const array1 = [
{"name": "connor", "age": 29, "sex": "M"},
{"name": "markus", "age": 32, "sex": "M"},
{"name": "kara", "age": 26, "sex": "F"}];

const newArray1 = array1.DynSort("age");

console.log(newArray1);

// Should return:

newArray1(
{"name": "kara", "age": 26, "sex": "F"},
{"name": "connor", "age": 29, "sex": "M"},
{"name": "markus", "age": 32, "sex": "M"});


Log

JMAC a | 17-05-25

Method introduced.

JMAC g_b | 01-07-25

Experimental fallback introduced for type.