Skip to content

Latest commit

 

History

History
223 lines (192 loc) · 3.74 KB

sort.md

File metadata and controls

223 lines (192 loc) · 3.74 KB

sort

The sort processor sorts the data by a given field that has numbers, dates, or strings as values. This will sort DataEntity or DataWindows.

Usage

Simple number sorting

Example of a job that uses the sort processor sorting on number values

{
    "name" : "testing",
    "workers" : 1,
    "slicers" : 1,
    "lifecycle" : "once",
    "assets" : [
        "standard"
    ],
    "operations" : [
        {
            "_op": "test-reader"
        },
        {
            "_op": "sort",
            "field": "id"
        }
    ]
}

Output of example job

const data = [
    {
        id: 2,
        date: '2019-05-03T20:02:00.000Z'
    },
    {
        id: 1,
        date: '2019-05-03T20:01:00.000Z'
    },
    {
        id: 4,
        date: '2019-05-03T20:01:00.000Z'
    },
    {
        id: 3,
        date: '2019-05-03T20:03:00.000Z'
    }
];

const results = await processor.run(data);

results === [
    {
        id: 1,
        date: '2019-05-03T20:01:00.000Z'
    },
    {
        id: 2,
        date: '2019-05-03T20:02:00.000Z'
    },
    {
        id: 3,
        date: '2019-05-03T20:03:00.000Z'
    },
    {
        id: 4,
        date: '2019-05-03T20:01:00.000Z'
    }
]

Simple date sorting

Example of a job that uses the sort processor sorting on date values

{
    "name" : "testing",
    "workers" : 1,
    "slicers" : 1,
    "lifecycle" : "once",
    "assets" : [
        "standard"
    ],
    "operations" : [
        {
            "_op": "test-reader"
        },
        {
            "_op": "sort",
            "field": "date"
        }
    ]
}

Output of example job

const data = [
    {
        id: 2,
        date: '2019-05-03T20:02:00.000Z'
    },
    {
        id: 1,
        date: '2019-05-03T20:01:00.000Z'
    },
    {
        id: 4,
        date: '2019-05-03T20:01:00.000Z'
    },
    {
        id: 3,
        date: '2019-05-03T20:03:00.000Z'
    }
];

const results = await processor.run(data);

results === [
    {
        id: 1,
        date: '2019-05-03T20:01:00.000Z'
    },
    {
        id: 4,
        date: '2019-05-03T20:01:00.000Z'
    },
    {
        id: 2,
        date: '2019-05-03T20:02:00.000Z'
    },
    {
        id: 3,
        date: '2019-05-03T20:03:00.000Z'
    }
]

Sort DataWindows by descending values

Example of a job sorting on DataWindows by desc

{
    "name" : "testing",
    "workers" : 1,
    "slicers" : 1,
    "lifecycle" : "once",
    "assets" : [
        "standard"
    ],
    "operations" : [
        {
            "_op": "test-reader"
        },
        {
            "_op": "sort",
            "field": "id",
            "order": "desc"
        }
    ]
}

Output of example job

const data = [
    {
        dataArray: [
            { id: 12 },
            { id: 2 },
            { id: 245 }
        ]
    },
    {
        dataArray: [
            { id: 143 },
            { id: 66321 },
            { id: 83872 }
        ]
    }
]

const results = await processor.run(data);

results === [
    {
        dataArray: [
            { id: 245 },
            { id: 12 },
            { id: 2 },
        ]
    },
    {
        dataArray: [
            { id: 83872 },
            { id: 66321 },
            { id: 143 },
        ]
    }
]

Parameters

Configuration Description Type Notes
_op Name of operation, it must reflect the exact name of the file String required
field The field in the input records to use for sorting String required
order The order in which it will be sorted (asc or desc) String optional, defaults to asc