-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathgetCountriesSortedByPopulation.js
More file actions
65 lines (59 loc) · 1.43 KB
/
getCountriesSortedByPopulation.js
File metadata and controls
65 lines (59 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Returns an array of country names sorted in descending order by population (biggest to smallest)
*
* @param {object[]} arr - The input array. Objects will be in the form: { country: "foo", population: 10 }
* @returns {string[]} - Returns an array of country names, sorted by their population in descending order (biggest to smallest)
*
* ex: getCountriesSortedByPopulation([ { country: "Denmark", population: 6 }, { country: "China", population: 1386 }, { country: "Egypt", population: 145 }])
* returns: ["China", "Egypt", "Denmark"]
*
*/
//takes in an array of objects
//and returns sorted by population value. (possibly using .sort)
let ex1 = [
{
country: "Algeria",
population: 41,
},
{
country: "Belize",
population: 0.4,
},
{
country: "China",
population: 1386,
},
{
country: "Denmark",
population: 6,
},
]
let ex2 = [
{
country: "Argentina",
population: 58,
},
{
country: "Egypt",
population: 145,
},
{
country: "Russia",
population: 1386,
},
{
country: "New Zealand",
population: 66,
},
]
function getCountriesSortedByPopulation(arr) {
}
let sorted = []
for(let key in arr) {
sorted.push(key)
}
sorted.sort(function(a,b) {
return b-a
})
module.exports = getCountriesSortedByPopulation
console.log(getCountriesSortedByPopulation(ex1))