I have an array containing multiple objects with multiple fields, and I'm trying to sort the array in decending order from the largest "Views" value to the lowest. but when I run my code it outputs a few in order, but the majority are random. There are a lot more array rows in my project if that could be affecting it.
list = [{title: "Test1", views: "25"}, {title: "Test2", "105"}, {title: "Test3", views: "12"}]
var len = list.length - 1;
list.sort(function (a, b) {
return a.views.localeCompare(b.views) || b.views - a.views;
});
for(var x = 0; x < len; x++) {
displayFunc(list[x].title, list[x].views);
}
Desired output:
{title: "Test2", views: "105"}, {title: "Test1", "25"}, {title: "Test3", views: "12"}
3 Answers 3
As mentioned in the comments, you dont need a localecompare here:
const list = [{title: "Test1", views: "25"}, {title: "Test2", views: "105"}, {title: "Test3", views: "12"}]
console.log(list.sort((a,b)=>{
return b.views - a.views
}));
-
When your anonymous method implementation is a single statement you can omit the
return
and curly brackets. This can be more simply written asconsole.log(list.sort((a,b)=>b.views - a.views))
2022年01月18日 11:08:54 +00:00Commented Jan 18, 2022 at 11:08
We can use any key available in object to sort. As per your requirement, 'view' is used here,
const list = [{title: "Test1", views: "25"}, {title: "Test2", views: "105"}, {title: "Test3", views: "12"}]
console.log(list.sort((a, b) => b.views - a.views));
You have some errors in your code. I corrected your version and give you a somewhat better, more modern version.
const list = [{title: "Test1", views: "25"}, {title: "Test2", views: "105"}, {title: "Test3", views: "12"}];
var len = list.length;
const list_sorted = list.sort(function (a, b) {
return parseInt(b.views, 10) - parseInt(a.views, 10);
});
for(var x = 0; x < len; x++) {
console.log(list[x].title, list[x].views);
}
/// Better
list
.map(el => ({ ...el, views: parseInt(el.views, 10) }))
.sort((a,b) => b.views - a.views)
.forEach(el => console.log(el.title, el.views));
-
Always specify a radix when using
parseInt()
, it does not default to 10.pilchard– pilchard2022年01月18日 08:11:47 +00:00Commented Jan 18, 2022 at 8:11 -
Good one. Therefore linting is good,Christian– Christian2022年01月18日 08:58:44 +00:00Commented Jan 18, 2022 at 8:58
-
@pilchard "...it does not default to 10" - It does -> 19.2.5
parseInt ( string, radix )
-> Step 9Andreas– Andreas2022年01月18日 09:35:11 +00:00Commented Jan 18, 2022 at 9:35 -
@Andreas it's not a hard default but a fallback after parsing the input. If it always defaulted to 10
parseInt('0x10')
andparseInt('0x10', 10)
would return the same valuepilchard– pilchard2022年01月18日 12:17:23 +00:00Commented Jan 18, 2022 at 12:17 -
@pilchard "but a fallback after parsing the input" - How would that even make any sense? I first parse the input and then later in the execution I determine the value of the radix parameter? o.O Your example is a special case that is handled in step 10.Andreas– Andreas2022年01月18日 12:38:59 +00:00Commented Jan 18, 2022 at 12:38
.localeCompare()
for numbers?||
option is only triggered when the values are the same, and subtracting them will only yield 0 again. Just use the subtractionlist.sort((a, b) => b.views - a.views);