获取基于另一个数组的内容排序的数组

问题描述 投票:4回答:7

如何对allGames数组进行排序,使其排序为firstGames?

const allGames = [
    { id: 2, name: 'game2' },
    { id: 1, name: 'game1' },
    { id: 4, name: 'game4' },
    { id: 3, name: 'game3' },
]
const installedGames = [
    { id: 2, name: 'game2' },
    { id: 3, name: 'game3' }
]

const sorted = allGames.sort((a, b) => {
    return a.id - b.id // but this just sorts by id
});

基本上我想得到这个订单:

{ id: 2, name: 'game2' },
{ id: 3, name: 'game3' },
{ id: 1, name: 'game1' },
{ id: 4, name: 'game4' }
javascript arrays sorting
7个回答
6
投票

let allGames = [
    { id: 2, name: 'game2' },
    { id: 1, name: 'game1' },
    { id: 4, name: 'game4' },
    { id: 3, name: 'game3' },
]
let installedGames = [
    { id: 3, name: 'game3' },
    { id: 2, name: 'game2' },
    
]

// Step 1. sort both arrays

allGames = allGames.sort( (a,b) => a.id - b.id )
installedGames = installedGames.sort( (a,b) => a.id - b.id )

// Step 2. remove installedGames from allGames

const remainingGames = allGames.filter( game => !installedGames.find(g => g.name === game.name) )

// step 3. concatenate both

const sortedGames = installedGames.concat(remainingGames)

console.log(sortedGames)

另外,我建议改变模型。我建议只有一个,而不是有两个不同的数组,并为你的游戏添加一个installed属性。这将使您的生活更轻松:

const allGames = [
    { id: 2, name: 'game2', installed : true },
    { id: 1, name: 'game1', installed : false },
    { id: 4, name: 'game4', installed : false },
    { id: 3, name: 'game3', installed : true },
]

const sortedGames = allGames.sort((a, b) =>
     +b.installed - +a.installed || a.id - b.id
);

console.log(sortedGames)

6
投票

您可以使用sort方法和findIndex内部,然后检查索引是否为-1并按结果排序。

const allGames = [{ id: 2, name: 'game2' },{ id: 1, name: 'game1' },{ id: 4, name: 'game4' },{ id: 3, name: 'game3' },]
const installedGames = [{ id: 2, name: 'game2' },{ id: 3, name: 'game3' }]

allGames.sort((a, b) => {
  const ai = installedGames.findIndex(({id}) => id === a.id);
  const bi = installedGames.findIndex(({id}) => id === b.id)
  return (bi != -1) - (ai != -1) || ai - bi
});

console.log(allGames)

您还可以创建对象表单已安装的游戏并按该对象排序。

const allGames = [{ id: 2, name: 'game2' },{ id: 1, name: 'game1' },{ id: 4, name: 'game4' },{ id: 3, name: 'game3' },]
const installedGames = [{ id: 2, name: 'game2' },{ id: 3, name: 'game3' }]
const inst = installedGames.reduce((r, {id}, i) => Object.assign(r, {[id]: i}), {})

allGames.sort((a, b) => (b.id in inst) - (a.id in inst) || inst[a.id] - inst[b.id]);
console.log(allGames)

3
投票

这是我的尝试:

const allGames = [
    { id: 2, name: 'game2' },
    { id: 1, name: 'game1' },
    { id: 4, name: 'game4' },
    { id: 3, name: 'game3' },
]
const installedGames = [
    { id: 2, name: 'game2' },
    { id: 3, name: 'game3' }
];

var ids = installedGames.map(x => x.id);

const sorted = allGames.slice().sort((a, b) => {
    var pos_a = ids.indexOf(a.id);
    var pos_b = ids.indexOf(b.id);
    // compare both installed
    if (pos_a !== -1 && pos_b !== -1) {
      return pos_a - pos_b;
    }
    // compare installed and not installed
    if (pos_a != -1) {
      return -1;
    }
    // sort the rest
    return 1;
});
console.log(sorted);

3
投票

我认为这是解决这个问题的最快方法。

 const allGames = [
    { id: 2, name: 'game2' },
    { id: 1, name: 'game1' },
    { id: 4, name: 'game4' },
    { id: 3, name: 'game3' },
]
const installedGames = [
    { id: 2, name: 'game2' },
    { id: 3, name: 'game3' }
]

const output = [
    ...installedGames,
    ...allGames.filter( io => installedGames.findIndex(il => io.id === il.id) === -1 )
];

2
投票

比较ab的标准如下:

  • 如果两个项目都已安装或两个项目都未安装,则首先使用较小ID的项目
  • 否则(保证安装一个,其他不是这样)安装项目首先

const allGames = [
    { id: 2, name: 'game2' },
    { id: 1, name: 'game1' },
    { id: 4, name: 'game4' },
    { id: 3, name: 'game3' },
]
const installedGames = [
    { id: 2, name: 'game2' },
    { id: 3, name: 'game3' }
];
allGames.sort(function(a, b) {
    // findIndex returns quicker than filter
    var ai = installedGames.findIndex(game => game.id === a.id) >= 0;
    var bi = installedGames.findIndex(game => game.id === b.id) >= 0;

    // ai === bi   -> both items are installed or both items are not installed
    // a.id - b.id -> item with smaller id first
    // ai - bi     -> if 1 - 0 then return -1, if 0 - 1 then return +1
    return ai === bi ? (a.id - b.id) : -(ai - bi);
});
console.log(allGames);

2
投票

你可以尝试以下。

首先要提高性能,用keyid作为value数组中的index创建一个installedGames地图。然后根据map中存储的索引对第一个数组进行排序。

映射第二个数组并创建一个id数组。现在基于的排序第一个数组

const allGames = [{ id: 2, name: 'game2' },{ id: 1, name: 'game1' },{ id: 4, name: 'game4' },{ id: 3, name: 'game3' }];
const installedGames = [{ id: 2, name: 'game2' },{ id: 3, name: 'game3' }];

const obj = installedGames.reduce((a,c,i) => Object.assign(a, {[c.id]:i}), {});

allGames.sort((a,b) => {
  let aIndex = obj[a.id], bIndex = obj[b.id];
  return aIndex != undefined ? bIndex != undefined ? aIndex - bIndex : -1 : 1;
});
console.log(allGames);

0
投票

你可以试试这个

const allGames = [
    { id: 2, name: 'game2' },
    { id: 1, name: 'game1' },
    { id: 4, name: 'game4' },
    { id: 3, name: 'game3' },
]
const installedGames = [
    { id: 2, name: 'game2' },
    { id: 3, name: 'game3' }
]

const sorted = ((a, b) => {
    return a.id - b.id //this just sorts by id
});
var out  = [...installedGames.sort(sorted), ...allGames.filter(o=> !(installedGames.map(i => i.id).indexOf(o.id) > -1)).sort(sorted)]

console.log(out)
© www.soinside.com 2019 - 2024. All rights reserved.