json数组的排序.md

json数组的排序

const jsonSort = (data, key, t = 'asc') {
    const d = data;
    const type = {
        asc(a, b) {
            return a[key] > b[key] ? 1 : -1;
        },
        desc(a, b) {
            return a[key] > b[key] ? -1 : 1;
        },
    };
    const s = type[t.toLowerCase()];
    d.sort(s);
    return d;
}