Pluxbox Logo

Sort array block

Purpose

Sort a list based on an attribute/property of the items.

Input

  • Array
    Takes a list an unsorted list.

  • Path
    The path to the attribute/property you wish to sort by.

  • Reverse?
    If ticked, the order will be descending instead of ascending.

Output

  • List
    The input list but sorted.

Example

I wish to sort the following list:

[
    {
        "name": "Bulbasaur",
        "level": 58,
        "moves": 4
    },
    {
        "name": "Ditto",
        "level": 99,
        "moves": 1
    },
    {
        "name": "wigglytuff",
        "level": 69,
        "moves": 6
    },
    {
        "name": "Pidgeot",
        "level": 12,
        "moves": 2
    },
    {
        "name": "Rattata",
        "level": 15,
        "moves": 3
    },
    {
        "name": "Dodrio",
        "level": 13,
        "moves": 1
    }
]

graph LR
A([List]) --Array--> I(Sort Array)
B(['level']) --Path--> I
C([false]) --Reverse?--> I
I --List--> D([New list])

Gives the following list:

[
    {
        "name": "Pidgeot",
        "level": 12,
        "moves": 2
    },
    {
        "name": "Dodrio",
        "level": 13,
        "moves": 1
    },
    {
        "name": "Rattata",
        "level": 15,
        "moves": 3
    },
    {
        "name": "Bulbasaur",
        "level": 58,
        "moves": 4
    },
    {
        "name": "wigglytuff",
        "level": 69,
        "moves": 6
    },
    {
        "name": "Ditto",
        "level": 99,
        "moves": 1
    }
]

Caveats

  • If the list does not contain any objects and all the items are the same type, there's no need to need to set the path.