Created
January 20, 2017 18:46
-
-
Save BastinRobin/0c45cf277f3a0739ce9f90ebf71203af to your computer and use it in GitHub Desktop.
Sorting
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
| a = [ {name:'robin', ranking:10 }, { name: 'sam', ranking: 40 }, { name: 'binu', ranking: 12 } ]; | |
| // Using function | |
| function sort_by_key(array, key) { | |
| return array.sort(function(a, b) { | |
| var x = a[key]; var y = b[key]; | |
| return ((x < y) ? -1 : ((x > y) ? 1 : 0)); | |
| }); | |
| } | |
| let average = (array) => array.reduce((a, b) => a + b) / array.length; | |
| function avg(array, key) { | |
| return average(array.map(o => o[key])); | |
| } | |
| console.log(sort_by_key(a, 'ranking')); | |
| console.log(avg(a, 'ranking')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment