Improved sorting to be stable

This commit is contained in:
Sebastian Lay 2021-08-31 16:28:51 +02:00
parent 6f58484e5e
commit c1f9e350b8
No known key found for this signature in database
GPG Key ID: A699D9B80D6068AA

View File

@ -44,21 +44,19 @@ const app = new Vue({
var sortBy = this.sortBy; var sortBy = this.sortBy;
var sortDir = this.sortDir; var sortDir = this.sortDir;
var sorted = this.filteredfilms.sort(function (a, b) { var sorted = this.filteredfilms.sort(function (a, b) {
switch(sortBy) {
case 'popularity':
return a.popularity - b.popularity;
case 'name':
var first = a.name.toLowerCase() + a.format; var first = a.name.toLowerCase() + a.format;
var second = b.name.toLowerCase() + b.format; var second = b.name.toLowerCase() + b.format;
if(first < second) return -1; switch(sortBy) {
if(first > second) return 1; case 'popularity':
return 0; return a.popularity - b.popularity || first.localeCompare(second);
case 'name':
return first.localeCompare(second);
case 'price': case 'price':
return a.price - b.price; return a.price - b.price || first.localeCompare(second);
case 'iso': case 'iso':
return a.iso - b.iso; return a.iso - b.iso || first.localeCompare(second);
case 'date': case 'date':
return a.launched - b.launched; return a.launched - b.launched || first.localeCompare(second);
} }
}); });
if(sortDir === 'asc') { if(sortDir === 'asc') {