2

So I have this array of objects

[
 {
 '1485958472927784961': {
 name: 'bruno fiverr',
 points: 6,
 user_id: '1485958472927784961',
 tweets: [Array]
 },
 '1414575563323420679': {
 name: 'ju',
 points: 7,
 user_id: '1414575563323420679',
 tweets: [Array]
 }
 }
]

and I would like to sort this array by the number of points the user has. I am trying to get it working using array.sort with the following function:

var top10 = array.sort(function(a, b) { return a.points > b.points ? 1 : -1; }).slice(0, 10);

but all I get is the same array from start. is that possible?

Barmar
788k57 gold badges554 silver badges668 bronze badges
asked Jan 25, 2022 at 21:29
8
  • You have an extra backtick in .slice(0, 10`). That must be a copying error or you'd get a syntax error. Commented Jan 25, 2022 at 21:32
  • on my code it's normal,think stack changed it or something .slice(0, 10); Commented Jan 25, 2022 at 21:33
  • 1
    Your objects are not separate array elements. They array just has one element, and it's an object. Commented Jan 25, 2022 at 21:34
  • You could use Object.values(array[0]).sort... Commented Jan 25, 2022 at 21:35
  • @Barmar is it possible with that array or do I need to change everything? Commented Jan 25, 2022 at 21:35

2 Answers 2

2

You have an array with a single object whose values you want to sort by a property. For that you'll want to:

  1. Access the first object in the outer array with array[0]
  2. Extract the values of the object as an array with Object.values
  3. Sort the values in descending order with the sort function (a,b) => b.points - a.points
  4. Obtain your 10 elements with .slice(0,10)

const array = [
 {
 '1485958472927784961': {
 name: 'bruno fiverr',
 points: 6,
 user_id: '1485958472927784961',
 tweets: [Array]
 },
 '1414575563323420679': {
 name: 'ju',
 points: 7,
 user_id: '1414575563323420679',
 tweets: [Array]
 }
 }
];
const top10 = Object.values(array[0]).sort((a,b) => b.points - a.points).slice(0,10);
console.log(top10);

answered Jan 25, 2022 at 21:40
Sign up to request clarification or add additional context in comments.

Comments

1

The outermost array is useless here. I would change your data structure to an object with key-values:

const data = {
 '1485958472927784961': {
 name: 'bruno fiverr',
 points: 6,
 user_id: '1485958472927784961',
 tweets: [],
 },
 '1414575563323420679': {
 name: 'ju',
 points: 7,
 user_id: '1414575563323420679',
 tweets: [],
 },
};

From this you can just get all the values which becomes an array of the objects, then run a sort on that.

const data = {
 '1485958472927784961': {
 name: 'bruno fiverr',
 points: 6,
 user_id: '1485958472927784961',
 tweets: []
 },
 '1414575563323420679': {
 name: 'ju',
 points: 7,
 user_id: '1414575563323420679',
 tweets: []
 }
}
const sorted = Object.values(data).sort(function(a, b) {
 return a.points > b.points ? 1 : -1;
}).slice(0, 10);
console.log(sorted)

answered Jan 25, 2022 at 21:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.