fix sort logic, handle numeric values
This commit is contained in:
parent
382e22b070
commit
125fba583e
2 changed files with 14 additions and 2 deletions
|
@ -10,6 +10,7 @@ document.querySelectorAll('.accordion-checkbox').forEach(cbox => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// FIXME: maybe move to utils
|
||||||
function getCookie(name) {
|
function getCookie(name) {
|
||||||
var cookieValue = null;
|
var cookieValue = null;
|
||||||
if (document.cookie && document.cookie !== '') {
|
if (document.cookie && document.cookie !== '') {
|
||||||
|
|
|
@ -1,10 +1,21 @@
|
||||||
|
// FIXME: move to utils
|
||||||
|
// From: https://stackoverflow.com/a/175787
|
||||||
|
function isNumeric (str) {
|
||||||
|
return !isNaN(str) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
|
||||||
|
!isNaN(parseFloat(str)) // ...and ensure strings of whitespace fail
|
||||||
|
}
|
||||||
|
|
||||||
function selectChanged() {
|
function selectChanged() {
|
||||||
const sortValue = document.getElementById('sort-select').value
|
const sortValue = document.getElementById('sort-select').value
|
||||||
const $films = [...document.querySelectorAll('.film')]
|
const $films = [...document.querySelectorAll('.film')]
|
||||||
location.hash = sortValue
|
location.hash = sortValue
|
||||||
$films.sort((a, b) => {
|
$films.sort((a, b) => {
|
||||||
const aVal = a.dataset[sortValue]
|
let aVal = a.dataset[sortValue]
|
||||||
const bVal = b.dataset[sortValue]
|
let bVal = b.dataset[sortValue]
|
||||||
|
if (isNumeric(aVal) && isNumeric(bVal)) {
|
||||||
|
aVal = parseFloat(aVal)
|
||||||
|
bVal = parseFloat(bVal)
|
||||||
|
}
|
||||||
return aVal < bVal ? -1 : 1
|
return aVal < bVal ? -1 : 1
|
||||||
})
|
})
|
||||||
document.getElementById('films-list').innerHTML = ''
|
document.getElementById('films-list').innerHTML = ''
|
||||||
|
|
Loading…
Reference in a new issue