如何使排序功能解决这个问题?

我有对象的数组。每个对象都包含名称和user_id属性。

const queue = [
  {
    user: 1,
    song: "Song1",
  },
  {
    user: 1,
    song: "Song2",
  },
  {
    user: 2,
    song: "Song3",
  },
  {
    user: 1,
    song: "Song4",
  },
  {
    user: 3,
    song: "Song5",
  },
];

我想对它进行排序,使其看起来像这样

const queue = [
  {
    user: 1,
    song: "Song1",
  },
  {
    user: 2,
    song: "Song3",
  },
  {
    user: 3,
    song: "Song5",
  },
  {
    user: 1,
    song: "Song2",
  },

  {
    user: 1,
    song: "Song4",
  },

];

So. If user 1 adds 10 songs to queue, user 2 adds 1 song to queue and user 3 adds 3 songs to queue, queue will look like user_1_entry,user_2_entry,user_3_entry,user_1_entry,user_3_entry,user_1_entry, user_3_entry, user_1_entry [...]

我该如何解决这个问题?

评论