2

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

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

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}]
1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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:

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.