我们如何根据另一个对象数组值对对象数组进行排序,就像我们有一个对象数组一样:
var _userEnd = [{"userID":554,"tEnd":6},{"userID":597,"tEnd":3},{"userID":605,"tEnd":3},{"userID":617,"tEnd":1},{"userID":553,"tEnd":1},{"userID":616,"tEnd":1},{"userID":596,"tEnd":0}]
在这个索引1,2,3,4,5具有相同的键“tEnd”的值,即3和1所以我只需要根据另一个对象数组排序那些索引
var _profsort=[{"userID":596,"score":100},{"userID":616,"score":95},{"userID":553,"score":100},{"userID":617,"score":85},{"userID":605,"score":95},{"userID":597,"score":85},{"userID":554,"score":100}]
此处,用户ID 597,605,617,553,616
的分数值为85,95,85,100,95
所以根据得分,我想排序我的第一个数组 输出应该是:
[{"userID":554,"tEnd":6},{"userID":605,"tEnd":3},{"userID":597,"tEnd":3},{"userID":553,"tEnd":1},{"userID":616,"tEnd":1},{"userID":617,"tEnd":1},{"userID":596,"tEnd":0}]
答案 0 :(得分:2)
您可以获取tEnd
的增量并找到score
并获取该增量。
function getScore(uID) {
return (_profsort.find(({ userID }) => userID === uID) || { score: 0 }).score;
}
var _userEnd = [{ userID: 554, tEnd: 6 }, { userID: 597, tEnd: 3 }, { userID: 605, tEnd: 3 }, { userID: 617, tEnd: 1 }, { userID: 553, tEnd: 1 }, { userID: 616, tEnd: 1 }, { userID: 596, tEnd: 0 }],
_profsort = [{ userID: 596, score: 100 }, { userID: 616, score: 95 }, { userID: 553, score: 100 }, { userID: 617, score: 85 }, { userID: 605, score: 95 }, { userID: 597, score: 85 }, { userID: 554, score: 100 }];
_userEnd.sort(function (a, b) {
return b.tEnd - a.tEnd || getScore(b.userID) - getScore(a.userID);
});
console.log(_userEnd);

.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)
使用sort
和find
var fnGetScore = uid => _profsort.find(s => s.userID == uid).score; //method to get the score value based on userId
var output = _userEnd.sort( (a, b) =>
(b.tEnd - a.tEnd) ||
fnGetScore(b.userID) - fnGetScore(b.userID)) //compare tEnd first and if they are same, compare the score value
<强>演示强>
var _userEnd = [{
"userID": 554,
"tEnd": 6
}, {
"userID": 597,
"tEnd": 3
}, {
"userID": 605,
"tEnd": 3
}, {
"userID": 617,
"tEnd": 1
}, {
"userID": 553,
"tEnd": 1
}, {
"userID": 616,
"tEnd": 1
}, {
"userID": 596,
"tEnd": 0
}];
var _profsort = [{
"userID": 596,
"score": 100
}, {
"userID": 616,
"score": 95
}, {
"userID": 553,
"score": 100
}, {
"userID": 617,
"score": 85
}, {
"userID": 605,
"score": 95
}, {
"userID": 597,
"score": 85
}, {
"userID": 554,
"score": 100
}];
var fnGetScore = uid => _profsort.find(s => s.userID == uid).score;
var output = _userEnd.sort((a, b) => (b.tEnd - a.tEnd) || fnGetScore(b.userID) - fnGetScore(b.userID))
console.log(output);
&#13;
答案 2 :(得分:0)
使用score
按userID
创建Array.reduce
的对象。按tEnd
排序,如果score
按sortObject
userId
排序,则按var _userEnd = [{"userID":554,"tEnd":6},{"userID":597,"tEnd":3},{"userID":605,"tEnd":3},{"userID":617,"tEnd":1},{"userID":553,"tEnd":1},{"userID":616,"tEnd":1},{"userID":596,"tEnd":0}]
var _profsort=[{"userID":596,"score":100},{"userID":616,"score":95},{"userID":553,"score":100},{"userID":617,"score":85},{"userID":605,"score":95},{"userID":597,"score":85},{"userID":554,"score":100}]
var sortObj = _profsort.reduce((r, o) => {
r[o.userID] = o.score;
return r;
}, Object.create(null));
_userEnd.sort((a, b) => b.tEnd - a.tEnd || sortObj[b.userID] - sortObj[a.userID]);
console.log(_userEnd);
排序:
{{1}}