-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBook.js
46 lines (39 loc) · 913 Bytes
/
Book.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
module.exports = class Book {
/**
* @type string
*/
name;
/**
* @type string
*/
author;
constructor(name, author) {
this.name = name;
this.author = author;
}
/**
* BigO => O(1)
*/
getAuthorLastName() {
return this.author.split(" ", 2)[1] || ""; // safe flow when no last name exists
}
/**
* BigO => O(n) where n is the books.length
* @param {Books[]} books
*/
static orderByAuthorLastName(books = []) {
/**
*
* @param {Book} firstBook
* @param {Book} secondBook
* @returns
*/
function orderCriteria(firstBook, secondBook) {
const firstLastName = firstBook.getAuthorLastName().toLowerCase(),
secondLastName = secondBook.getAuthorLastName().toLowerCase();
return firstLastName.localeCompare(secondLastName);
}
const orderBooks = books.sort(orderCriteria);
return orderBooks;
}
};