最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to sort an array using values from an enum? - Stack Overflow

programmeradmin0浏览0评论

I want to sort elements of an array using an enum, I would like to know how to do it, I have tried with a switch statement with no success.

   const enum Order {
      Start = 'Start',
      Run = 'Run',
      End = 'End',
    }

    const predicate (a, b) => // TODO 

    const data = [Order.End, Order.Run, Order.Start]

    const result = data.sort(predicate)

    // wanted result is: // Start, Run, End

I want to sort elements of an array using an enum, I would like to know how to do it, I have tried with a switch statement with no success.

   const enum Order {
      Start = 'Start',
      Run = 'Run',
      End = 'End',
    }

    const predicate (a, b) => // TODO 

    const data = [Order.End, Order.Run, Order.Start]

    const result = data.sort(predicate)

    // wanted result is: // Start, Run, End
Share Improve this question asked Nov 26, 2018 at 9:52 RadexRadex 8,58724 gold badges60 silver badges96 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 13

Normally with an enum, the value is already comparable.

const enum Order {
    Start = 0,
    Run = 1,
    End = 2,
}

const data = [Order.End, Order.Run, Order.Start];

const result = data.sort();

console.log(result);

A non-constant enum can even be mapped to the string values, as shown here:

enum Order {
    Start = 0,
    Run = 1,
    End = 2,
}

const data = [Order.End, Order.Run, Order.Start];

const result = data.sort();

console.log(result.map((val) => Order[val]));

But in your case, you could convert them into an easily sortable value if necessary (assuming you desire to avoid alphabetical ordering).

const enum Order {
    Start = 'Start',
    Run = 'Run',
    End = 'End',
}

const predicate = (a, b) => {
    const map = {};
    map[Order.Start] = 1;
    map[Order.Run] = 2;
    map[Order.End] = 3;

    if (map[a] < map[b]) {
        return -1;
    }

    if (map[a] > map[b]) {
        return 1;
    }

    return 0;
}

const data = [Order.End, Order.Run, Order.Start];

const result = data.sort(predicate);

console.log(result);

I created a function in typescript based on previous answer to order objects from an enum.

export function sortByStatus(a: Element, b: Element): number {
    const map = new Map<Status, number>();
    map.set(Status.DONE, 0);
    map.set(Status.ERROR, 1);
    map.set(Status.PROCESSING, 2);
    map.set(Status.NONE, 3);

    if (map.get(a.status) < map.get(b.status)) {
        return -1;
    }
    if (map.get(a.status) > map.get(b.status)) {
        return 1;
    }
    return 0;
}

Furthermore, I include a mini test to check its functionality.

it('check sortByStatus', () => {
    expect(sortByStatus(a, b)).toBeLessThanOrEqual(1);
    expect(sortByStatus(b, a)).toBeGreaterThanOrEqual(-1);
    expect(sortByStatus(a, a)).toBe(0);
});

Here is the response I used:

const Order = {
  Start:'Start',
  Run: 'Run',
  End: 'End',
}

const orderValues = Object.values(Order)

const predicate = (a, b) =>
  orderValues.findIndex(value => a === value) - orderValues.findIndex(value => b === value)

const data = [Order.End, Order.Run, Order.Start]

const result = data.sort(predicate)

console.log(result)

发布评论

评论列表(0)

  1. 暂无评论