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

node.js - Javascript sort array of objects using array of priority - Stack Overflow

programmeradmin1浏览0评论

I have this array of objects:

var eventList = [
    {
        eventName: "abc",
        status: "pleted"
    },
    {
        eventName: "def",
        status: "live"
    },
    {
        eventName: "ghi",
        status: "live"
    },
    {
        eventName: "jkl",
        status: "uping"
    },
]

I want to sort these array of objects using a priority array of a specific key, say ["live", "uping", "pleted"] for status, meaning all live events e first, followed by uping followed by pleted. Answers all over the internet seem like you can only sort array objects using keys as ascending or descending. How to I approach this?

I have this array of objects:

var eventList = [
    {
        eventName: "abc",
        status: "pleted"
    },
    {
        eventName: "def",
        status: "live"
    },
    {
        eventName: "ghi",
        status: "live"
    },
    {
        eventName: "jkl",
        status: "uping"
    },
]

I want to sort these array of objects using a priority array of a specific key, say ["live", "uping", "pleted"] for status, meaning all live events e first, followed by uping followed by pleted. Answers all over the internet seem like you can only sort array objects using keys as ascending or descending. How to I approach this?

Share Improve this question asked Jan 8, 2021 at 12:10 ShriShri 7511 gold badge9 silver badges27 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 10

You could do it using Array.prototype.sort() method with an ordering array.

const eventList = [
  {
    eventName: 'abc',
    status: 'pleted',
  },
  {
    eventName: 'def',
    status: 'live',
  },
  {
    eventName: 'ghi',
    status: 'live',
  },
  {
    eventName: 'jkl',
    status: 'uping',
  },
];

const order = ['live', 'uping', 'pleted'];
eventList.sort((x, y) => order.indexOf(x.status) - order.indexOf(y.status));
console.log(eventList);

If you would want to make index searching faster when sorting you could use Map Object.

const eventList = [
  {
    eventName: 'abc',
    status: 'pleted',
  },
  {
    eventName: 'def',
    status: 'live',
  },
  {
    eventName: 'ghi',
    status: 'live',
  },
  {
    eventName: 'jkl',
    status: 'uping',
  },
];

const order = ['live', 'uping', 'pleted'];
const map = new Map();
order.forEach((x, i) => map.set(x, i));
eventList.sort((x, y) => map.get(x.status) - map.get(y.status));
console.log(eventList);

You can do this without sorting for a linear time plexity by first grouping your array based on status, and then using .flatMap() on your order array. For each value in the ordered array, you can grab the grouped value from the grouping in O(1) (ie: instantly), and map those grouped objects. As you're using flatMap these grouped objects will be flattened into the resulting array:

const eventList = [ { eventName: "abc", status: "pleted" }, { eventName: "def", status: "live" }, { eventName: "ghi", status: "live" }, { eventName: "jkl", status: "uping" }, ];

const order = ["live", "uping", "pleted"];
const grouped = eventList.reduce(
  (map, o) => map.set(o.status, (map.get(o.status) || []).concat(o)), new Map
);

const result = order.flatMap(status => grouped.get(status) || []);
console.log(result);

发布评论

评论列表(0)

  1. 暂无评论