-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSorting.html
More file actions
45 lines (36 loc) · 1.1 KB
/
Sorting.html
File metadata and controls
45 lines (36 loc) · 1.1 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body></body>
</html>
<script>
// sort() = mehtois used to sort elements of an array in place
// Sorts elements as string in lexicographic order, not alphabetical
// lexicographic = (alphabet + numbers + symbols) as strings
let arr = ["apple", "orange", "banana", "pineapple"]
arr.sort()
console.log(arr)
let numbers = [1, 10, 5, 25, 85, 49, 99, 665, 458]
numbers.sort((a, b) => a - b)
console.log(numbers)
numbers.sort((a, b) => b - a)
console.log(numbers)
const people = [
{ name: "Deep", age: 20, gpa: 3.0 },
{ name: "sandy", age: 26, gpa: 1.0 },
{ name: "jay", age: 32, gpa: 2.5 },
{ name: "dhruv", age: 24, gpa: 4.0 },
]
people.sort((a, b) => a.name - b.name)
console.log(people)
people.sort((a, b) => a.age - b.age)
console.log(people)
people.sort((a, b) => a.gpa - b.gpa)
console.log(people)
people.sort((a, b) => b.name.localeCompare(a.name))
console.log(people)
</script>