2

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
  • 2
    Why .localeCompare() for numbers? Commented Jan 18, 2022 at 7:51
  • 1
    localeCompare is sorting them in alphabetic order, your || option is only triggered when the values are the same, and subtracting them will only yield 0 again. Just use the subtraction list.sort((a, b) => b.views - a.views); Commented Jan 18, 2022 at 7:54
  • @pilchard Ahhh, thank you so much! I originally tried using 'list.sort()' but I didn't set it up properly so ended up switching to '.localeCompare()' hoping that would work. Thank you very much, I really appreciate it! Commented Jan 18, 2022 at 8:07

3 Answers 3

4

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
}));

Sign up to request clarification or add additional context in comments.

1 Comment

When your anonymous method implementation is a single statement you can omit the return and curly brackets. This can be more simply written as console.log(list.sort((a,b)=>b.views - a.views))
0

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));

Comments

0

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));

7 Comments

Always specify a radix when using parseInt(), it does not default to 10.
Good one. Therefore linting is good,
@pilchard "...it does not default to 10" - It does -> 19.2.5 parseInt ( string, radix ) -> Step 9
@Andreas it's not a hard default but a fallback after parsing the input. If it always defaulted to 10 parseInt('0x10') and parseInt('0x10', 10) would return the same value
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.