How can we sort the object array based on another object array value, like we have an object array:
如何根据另一个对象数组值对对象数组进行排序,就像我们有一个对象数组:
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}]
In this the index 1,2,3,4,5 are having the same value of the key "tEnd" i.e., 3 and 1 so I just need to sort only those index on the basis of another object array
在这里,索引1、2、3、4、5的值与key“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}]
Here the userID 597,605,617,553,616
are having the score value 85,95,85,100,95
这里的userID 597,605,617,553,616的分数是85,95,85,100,95
so based on score, I want to sort my first array Output should be:
基于分数,我想排序我的第一个数组输出应该是:
[{"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}]
3 个解决方案
#1
0
Create an object of score
by userID
using Array.reduce
. Sort by tEnd
, and if it's equal sort by the score
you get from the sortObject
by the userId
:
使用Array.reduce使用userID创建一个score对象。按倾向排序,如果按用户id从sortObject中得到的分数排序:
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);
#2
2
You could take the delta of tEnd
and find the score
and take that delta.
你可以取倾斜度的值然后取这个值。
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; }
#3
1
Use sort
and 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
Demo
演示
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);
#1
0
Create an object of score
by userID
using Array.reduce
. Sort by tEnd
, and if it's equal sort by the score
you get from the sortObject
by the userId
:
使用Array.reduce使用userID创建一个score对象。按倾向排序,如果按用户id从sortObject中得到的分数排序:
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);
#2
2
You could take the delta of tEnd
and find the score
and take that delta.
你可以取倾斜度的值然后取这个值。
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; }
#3
1
Use sort
and 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
Demo
演示
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);