From c1f9e350b83125c8d683e75cbdbf0dbe0f1cdb85 Mon Sep 17 00:00:00 2001 From: Sebastian Lay Date: Tue, 31 Aug 2021 16:28:51 +0200 Subject: [PATCH] Improved sorting to be stable --- js/script-index.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/js/script-index.js b/js/script-index.js index 8eafed9..01d1850 100644 --- a/js/script-index.js +++ b/js/script-index.js @@ -44,21 +44,19 @@ const app = new Vue({ var sortBy = this.sortBy; var sortDir = this.sortDir; var sorted = this.filteredfilms.sort(function (a, b) { + var first = a.name.toLowerCase() + a.format; + var second = b.name.toLowerCase() + b.format; switch(sortBy) { case 'popularity': - return a.popularity - b.popularity; + return a.popularity - b.popularity || first.localeCompare(second); case 'name': - var first = a.name.toLowerCase() + a.format; - var second = b.name.toLowerCase() + b.format; - if(first < second) return -1; - if(first > second) return 1; - return 0; + return first.localeCompare(second); case 'price': - return a.price - b.price; + return a.price - b.price || first.localeCompare(second); case 'iso': - return a.iso - b.iso; + return a.iso - b.iso || first.localeCompare(second); case 'date': - return a.launched - b.launched; + return a.launched - b.launched || first.localeCompare(second); } }); if(sortDir === 'asc') {