Last active
June 23, 2018 23:28
-
-
Save vicsstar/1b2baa18434120891a5f7d0b083bb8b3 to your computer and use it in GitHub Desktop.
Ordering and Rankings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function inRankingOrder(arr) { | |
return arr.sort((obj1, obj2) => { | |
const ranking1 = obj1.ranking, | |
ranking2 = obj2.ranking; | |
return ranking1 < ranking2 ? -1 : (ranking1 > ranking2 ? 1 : 0); | |
}); | |
} | |
function averageRanking(arr) { | |
const sum = arr.map(obj => obj.ranking). | |
reduce((num1, num2) => num1 + num2); | |
return sum / arr.length; | |
} | |
const array = [ | |
{ name: 'John', ranking: 5 }, | |
{ name: 'Victor', ranking: 7 }, | |
{ name: 'Joy', ranking: 7 }, | |
{ name: 'Melbury', ranking: 4 } | |
]; | |
console.log(inRankingOrder(array)); | |
console.log(averageRanking(array)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment