Sort
Sorts a list of objects by given key.
๐ฌ Usage
<script>
import {sort} from "@sveu/shared/lists"
const list = [
{id: 1, name: "John"},
{id: 2, name: "Jane"},
{id: 3, name: "Mila"},
{id: 4, name: "Emma"},
{id: 5, name: "Jack"},
]
const sorted = sort(list, "name") // [{id: 4, name: "Emma"}, {id: 5, name: "Jack"}, {id: 2, name: "Jane"}, {id: 1, name: "John"}, {id: 3, name: "Mila"}]
const sorted_desc = sort(list, "-name") // [{id: 3, name: "Mila"}, {id: 1, name: "John"}, {id: 2, name: "Jane"}, {id: 5, name: "Jack"}, {id: 4, name: "Emma"}]
</script>
๐ฉโ๐ปAPI
๐ป Arguments
Name | Description | Type | Required |
---|---|---|---|
list | The list to sort | T[] | Yes |
key | The key to sort by | string | Yes |
โฉ๏ธ Returns
A sorted list of objects.
๐งช Playground
Source Code ๐
Source Code
import type { Dict } from "../../utils"
/**
* Sorts a list of objects by a given key.
*
* @param list - The list to sort.
*
* @param key - The key to sort by.
*
* @returns The sorted list.
*/
export function sort<T extends Dict>(list: T[], key: string) {
if (!list) return []
let sort_order = 1
if (key[0] === "-") {
sort_order = -1
key = key.slice(1)
}
const order = (a: T, b: T) => {
const result = a[key] < b[key] ? -1 : a[key] > b[key] ? 1 : 0
return result * sort_order
}
return list.slice().sort(order)
}
Last update: 2023-02-14
Authors: Mohamed-Kaizen