-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBD2.4-HW-1.js
55 lines (43 loc) · 1.6 KB
/
BD2.4-HW-1.js
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
const express = require('express');
const app = express();
let heights = [160, 175, 180, 165, 170];
let employees = [
{ name: 'Rahul', employeeId: '101', salary: 50000 },
{ name: 'Sita', employeeId: '102', salary: 60000 },
{ name: 'Amit', employeeId: '103', salary: 55000 }
];
let books = [
{ title: 'The God of Small Things', author: 'Arundhati Roy', pages: 340 },
{ title: 'The White Tiger', author: 'Aravind Adiga', pages: 318 },
{ title: 'The Palace of Illusions', author: 'Chitra Banerjee Divakaruni', pages: 360 }
];
function sortHeightsAscending(heights) {
return heights.sort((a, b) => a - b);
}
app.get('/heights/sort-ascending', (req, res) => {
let sortedHeights = sortHeightsAscending([...heights]); //using spread operator :)
res.json(sortedHeights);
});
function sortHeightsDescending(heights) {
return heights.sort((a, b) => b - a);
}
app.get('/heights/sort-descending', (req, res) => {
let sortedHeights = sortHeightsDescending([...heights]);
res.json(sortedHeights);
});
function sortEmployeesBySalaryDescending(employees) {
return employees.sort((a, b) => b.salary - a.salary);
}
app.get('/employees/sort-by-salary-descending', (req, res) => {
const sortedEmployees = sortEmployeesBySalaryDescending([...employees]);
res.json(sortedEmployees);
});
function sortBooksByPagesAscending(books) {
return books.sort((a, b) => a.pages - b.pages);
}
app.get('/books/sort-by-pages-ascending', (req, res) => {
const sortedBooks = sortBooksByPagesAscending([...books]);
res.json(sortedBooks);
});
const PORT = 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));