Untitled

mail@pastecode.io avatar
unknown
typescript
5 months ago
743 B
3
Indexable
export function arraySort<T>(array: T[], oDataConfig: ODataConfig) {
    const sorts = oDataConfig.Sorts.sort((a, b) => b.Priority - a.Priority);

    return sorts.reduce(
        (sortedArray, sort) => {
            return sortedArray.sort((a, b) => {
                const valueA = a[sort.PropertyName as keyof T];
                const valueB = b[sort.PropertyName as keyof T];

                if (valueA < valueB) {
                    return sort.Direction === ODataSortingDirection.Descending ? -1 : 1;
                } else if (valueA > valueB) {
                    return sort.Direction === ODataSortingDirection.Ascending ? 1 : -1;
                }
                return 0;
            });
        },
        [...array],
    );
}
Leave a Comment