获取根据另一个数组的内容排序的数组

时间:2019-04-24 11:16:21

标签: javascript arrays sorting

我该如何对allGames数组进行排序以使 installedGames首先?

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' }

7 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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)

答案 2 :(得分: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 :(得分: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 )
];

答案 4 :(得分:2)

您可以尝试关注。

首先,为了提高性能,请在key数组中创建一个idvalue并以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);

答案 5 :(得分: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);

答案 6 :(得分: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)