1

I'm trying to write a custom sort function for my array of objects in javascript. For testing purposes, my arr array looks like this:

[{
  _id: '5798afda8830efa02be8201e',
  type: 'PCR',
  personId: '5798ae85db45cfc0130d864a',
  numberOfVotes: 1,
  __v: 0
}, {
  _id: '5798afad8830efa02be8201d',
  type: 'PRM',
  personId: '5798aedadb45cfc0130d864b',
  numberOfVotes: 7,
  __v: 0
}]

I want to sort the objects using this function(the criteria is numberOfVotes):

arr.sort(function(a, b) {
  if (a.numberOfVotes > b.numberOfVotes) {
    return 1;
  }
  if (b.numberOfVotes > a.numberOfVotes) {
    return -1;
  } else return 0;
});

When i print the results, I receive the same order like before, aka 5798afda8830efa02be8201e,5798afad8830efa02be8201d

Am I missing something?

7
  • 2
    Your input array is already sorted by your criteria (numberOfVotes). What did you expect to happen? Commented Aug 12, 2016 at 9:34
  • @melpomene i want it to be sorted descreasingly. and if i replace the "<" with ">", i receive the same result Commented Aug 12, 2016 at 9:36
  • 2
    Try arr.sort(function (a, b) { return b.numberOfVotes - a.numberOfVotes; });. Commented Aug 12, 2016 at 9:37
  • 2
    @Issue429 if you change both signs to < you'll get the opposite order Commented Aug 12, 2016 at 9:38
  • 1
    @Issue429 So show your actual code. Commented Aug 12, 2016 at 9:41

2 Answers 2

2

If you want to sort by descending order of votes:

var arr = [{_id: '5798afda8830efa02be8201e',type: 'PCR',personId: '5798ae85db45cfc0130d864a',numberOfVotes: 1,__v: 0}, {_id: '5798afad8830efa02be8201d',type: 'PRM',personId: '5798aedadb45cfc0130d864b',numberOfVotes: 7,__v: 0}];

arr.sort(function(a, b) {
  return b.numberOfVotes - a.numberOfVotes;
});

console.log(arr);

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

1 Comment

i've marked this answer as right because it is smarter made, even though the ideea was @melpomene 's
1

I guess you want to sort by descending order of votes.

You need to change the condition in if block. Also note id like 5798afda8830efa02be8201e is wrong, It need to be string '5798afda8830efa02be8201e'

    var arr=[{
      _id: '5798afda8830efa02be8201e',
      type: 'PCR',
      personId: '5798ae85db45cfc0130d864a',
      numberOfVotes: 1,
      __v: 0
    }, {
      _id: '5798afad8830efa02be8201d',
      type: 'PRM',
      personId: '5798aedadb45cfc0130d864b',
      numberOfVotes: 7,
      __v: 0
    }]
    
    arr.sort( function ( a, b ) {
          if ( a.numberOfVotes < b.numberOfVotes ) {
            return 1;
          }
          else if ( b.numberOfVotes < a.numberOfVotes ) {
            return -1;
          } else{return 0;}
       });
    
    console.log(arr)

JSFIDDLE

Comments

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.