基于另一个数组对数组进行排序的最快方法

时间:2016-06-03 04:43:18

标签: javascript

下面是我的两个数组,基于另一个数组对一个数组进行排序的最快方法是什么。

var MainArray = [{
  guid: '24c2b868-8f4d-4113-a537-f8e7f57bb88b',
  info: 'photo 1'
}, {
  guid: '27e910cf-ba4c-412c-9faf-e7db5f058639',
  info: 'photo 2'
}, {
  guid: '66fc9e12-1c0a-447c-81c8-2f3a4bf0608c',
  info: 'photo 3'
}, {
  guid: 'f2afa8a9-6666-44d5-b360-8e7b8fb27c2e',
  info: 'photo 4'
}, {
  guid: '6d670044-9d3e-4c48-ba2b-5ec3e0c6be0f',
  info: 'photo 5'
}];




var newArray = [{
  id: 1,
  name: 'name1',
  guid: '6d670044-9d3e-4c48-ba2b-5ec3e0c6be0f'
}, {
  id: 2,
  name: 'name2',
  guid: 'f2afa8a9-6666-44d5-b360-8e7b8fb27c2e'
}, {
  id: 3,
  name: 'name3',
  guid: '66fc9e12-1c0a-447c-81c8-2f3a4bf0608c'
}, {
  id: 4,
  name: 'name4',
  guid: '27e910cf-ba4c-412c-9faf-e7db5f058639'
}, {
  id: 5,
  name: 'name5',
  guid: '24c2b868-8f4d-4113-a537-f8e7f57bb88b'
}];

我希望根据MainArray的guid对新数组进行排序,我该怎么做?

3 个答案:

答案 0 :(得分:1)

生成一个对象,该对象引用主数组中的索引并根据该

进行排序
// object for storing reference to index
var obj = {};

// iterate and store index reference in object
MainArray.forEach(function(v, i) {
  obj[v.guid] = i;
});

// sort based on the index
newArray.sort(function(a, b) {
  return obj[a.guid] - obj[b.guid];
});

var MainArray = [{
  guid: '24c2b868-8f4d-4113-a537-f8e7f57bb88b',
  info: 'photo 1'
}, {
  guid: '27e910cf-ba4c-412c-9faf-e7db5f058639',
  info: 'photo 2'
}, {
  guid: '66fc9e12-1c0a-447c-81c8-2f3a4bf0608c',
  info: 'photo 3'
}, {
  guid: 'f2afa8a9-6666-44d5-b360-8e7b8fb27c2e',
  info: 'photo 4'
}, {
  guid: '6d670044-9d3e-4c48-ba2b-5ec3e0c6be0f',
  info: 'photo 5'
}];




var newArray = [{
  id: 1,
  name: 'name1',
  guid: '6d670044-9d3e-4c48-ba2b-5ec3e0c6be0f'
}, {
  id: 2,
  name: 'name2',
  guid: 'f2afa8a9-6666-44d5-b360-8e7b8fb27c2e'
}, {
  id: 3,
  name: 'name3',
  guid: '66fc9e12-1c0a-447c-81c8-2f3a4bf0608c'
}, {
  id: 4,
  name: 'name4',
  guid: '27e910cf-ba4c-412c-9faf-e7db5f058639'
}, {
  id: 5,
  name: 'name5',
  guid: '24c2b868-8f4d-4113-a537-f8e7f57bb88b'
}];

var obj = {};

MainArray.forEach(function(v, i) {
  obj[v.guid] = i;
});

newArray.sort(function(a, b) {
  return obj[a.guid] - obj[b.guid];
});

console.log(newArray);

答案 1 :(得分:0)

尝试使用将guid值映射到信息值的地图:

var MainArray = [ { guid : '24c2b868-8f4d-4113-a537-f8e7f57bb88b',info : 'photo 1'},
                  { guid : '27e910cf-ba4c-412c-9faf-e7db5f058639',info : 'photo 2'},
                  { guid : '66fc9e12-1c0a-447c-81c8-2f3a4bf0608c',info : 'photo 3'},
                  { guid : 'f2afa8a9-6666-44d5-b360-8e7b8fb27c2e',info : 'photo 4'},
                  { guid : '6d670044-9d3e-4c48-ba2b-5ec3e0c6be0f',info : 'photo 5'}
                   ];

var newArray = [{id : 1,name : 'name1',guid:'6d670044-9d3e-4c48-ba2b-5ec3e0c6be0f'},
               {id : 2,name : 'name2',guid:'f2afa8a9-6666-44d5-b360-8e7b8fb27c2e'},
               {id : 3,name : 'name3',guid:'66fc9e12-1c0a-447c-81c8-2f3a4bf0608c'},
               {id : 4,name : 'name4',guid:'27e910cf-ba4c-412c-9faf-e7db5f058639'},
               {id : 5,name : 'name5',guid:'24c2b868-8f4d-4113-a537-f8e7f57bb88b'}];

var map = {};

for (var i = 0; i < MainArray.length; i++) {
  map[MainArray[i].guid] = MainArray[i].info;
}

newArray.sort(function(a,b){return map[a.guid].localeCompare(map[b.guid]);})

答案 2 :(得分:0)

Pranav C Balan 的回答有效,我想分享另一种解决方法。

// mapping over MainArray since it contains sorted order
let sortedArr = MainArray.map((mainArrObject,i) => {

//filtering each newArray object based on MainArray index
 return newArray.filter(newArrObject => {
  return newArrObject.guid == MainArray[i].guid
 })[0]

})


console.log(sortedArr)

var MainArray = [{
  guid: '24c2b868-8f4d-4113-a537-f8e7f57bb88b',
  info: 'photo 1'
}, {
  guid: '27e910cf-ba4c-412c-9faf-e7db5f058639',
  info: 'photo 2'
}, {
  guid: '66fc9e12-1c0a-447c-81c8-2f3a4bf0608c',
  info: 'photo 3'
}, {
  guid: 'f2afa8a9-6666-44d5-b360-8e7b8fb27c2e',
  info: 'photo 4'
}, {
  guid: '6d670044-9d3e-4c48-ba2b-5ec3e0c6be0f',
  info: 'photo 5'
}];

var newArray = [{
  id: 1,
  name: 'name1',
  guid: '6d670044-9d3e-4c48-ba2b-5ec3e0c6be0f'
}, {
  id: 2,
  name: 'name2',
  guid: 'f2afa8a9-6666-44d5-b360-8e7b8fb27c2e'
}, {
  id: 3,
  name: 'name3',
  guid: '66fc9e12-1c0a-447c-81c8-2f3a4bf0608c'
}, {
  id: 4,
  name: 'name4',
  guid: '27e910cf-ba4c-412c-9faf-e7db5f058639'
}, {
  id: 5,
  name: 'name5',
  guid: '24c2b868-8f4d-4113-a537-f8e7f57bb88b'
}];

let sortedArr = MainArray.map((mainArrObject,i) => {
 return newArray.filter(newArrObject => {
  return newArrObject.guid == MainArray[i].guid
 })[0]

})

console.log(sortedArr)