| 36 | {{{#!sql |
| 37 | function sortBooks(books, sortType) { |
| 38 | const sortedBooks = [...books]; |
| 39 | |
| 40 | switch(sortType) { |
| 41 | case 'name-asc': |
| 42 | sortedBooks.sort((a, b) => a.title.localeCompare(b.title)); |
| 43 | break; |
| 44 | case 'name-desc': |
| 45 | sortedBooks.sort((a, b) => b.title.localeCompare(a.title)); |
| 46 | break; |
| 47 | case 'year-desc': |
| 48 | sortedBooks.sort((a, b) => b.publishedYear - a.publishedYear); |
| 49 | break; |
| 50 | case 'year-asc': |
| 51 | sortedBooks.sort((a, b) => a.publishedYear - b.publishedYear); |
| 52 | break; |
| 53 | case 'relevance': |
| 54 | default: |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | return sortedBooks; |
| 59 | } |
| 60 | }}} |
| 61 | |