From 125fba583e51c2d1a4a0626c14176602355bc00b Mon Sep 17 00:00:00 2001 From: Sanjay B Date: Thu, 28 Oct 2021 22:47:34 +0530 Subject: [PATCH] fix sort logic, handle numeric values --- app/static/js/about.js | 1 + app/static/js/films.js | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/static/js/about.js b/app/static/js/about.js index ca22c44..d785bfb 100755 --- a/app/static/js/about.js +++ b/app/static/js/about.js @@ -10,6 +10,7 @@ document.querySelectorAll('.accordion-checkbox').forEach(cbox => { }) }) +// FIXME: maybe move to utils function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie !== '') { diff --git a/app/static/js/films.js b/app/static/js/films.js index 8eec4cd..1b055fe 100644 --- a/app/static/js/films.js +++ b/app/static/js/films.js @@ -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() { const sortValue = document.getElementById('sort-select').value const $films = [...document.querySelectorAll('.film')] location.hash = sortValue $films.sort((a, b) => { - const aVal = a.dataset[sortValue] - const bVal = b.dataset[sortValue] + let aVal = a.dataset[sortValue] + let bVal = b.dataset[sortValue] + if (isNumeric(aVal) && isNumeric(bVal)) { + aVal = parseFloat(aVal) + bVal = parseFloat(bVal) + } return aVal < bVal ? -1 : 1 }) document.getElementById('films-list').innerHTML = ''