简体   繁体   中英

Sort an array using ratio

I am trying to sort an array by the win and lose ratio of the players.

My code looks like this actually:

 const array = [{playerName: 'toto', win: 2, lose: 2}, {playerName: 'titi', win: 0, lose: 0}, {playerName: 'tata', win: 3, lose: 1}]; array.sort((a, b) => a.win / a.lose || b.win / b.lose); console.log(array);

The player 'titi' is above the player 'toto' inside the sorted array and I don't know why.

You have a couple of issues with your sort function. Firstly, you are dividing by a.lose and b.lose which for your sample data are both 0 so those division will return Infinity . Secondly, a sort callback should return a negative number, 0 , or a positive number dependent on whether a should be sorted lower than, equal to or greater than b respectively and yours - due to your logic - will always return a positive number. I think what you actually want is:

 const array = [{ playerName: 'toto', win: 2, lose: 2 }, { playerName: 'titi', win: 0, lose: 0 }, { playerName: 'tata', win: 3, lose: 1 }]; array.sort((a, b) => { if (a.win + a.lose == 0) return 1; if (b.win + b.lose == 0) return -1; return b.win / (b.win + b.lose) - a.win / (a.win + a.lose); }); console.log(array);

look here

to be more specific:
If compareFunction(a, b) returns greater than 0, sort b to an index lower than a (ie b comes first).

in your case its 1/0 which values to Infinity which is true value thus it never calculates the b part. when you return true it's the same as returning 1.

I found your problem solution for sorting array. this way you easy to understend. this code answer is descending order of win or lose index parameter. Some small change of your code is following:

 const array = [ {playerName: 'toto', win: 1, lose: 0}, {playerName: 'titi', win: 0, lose: 0} ]; array.sort((a, b) => { if(a.win > b.win || a.lose < b.lose) { return 1; } return -1; }); console.log(array);

if you have to change order then replace "<" with ">" and ">" with "<".

Take cases where sum of wins and loses is zero wiht comparing function

const sortPlayers =(a, b) => {
  if (a.win + a.lose + b.win + b.lose === 0) {
    return 0
  } else if (a.win + a.lose === 0 && b.win + b.lose !== 0) {
    return 1
  } else if (a.win + a.lose !== 0 && b.win + b.lose === 0) {
    return -1
  } else if ((a.win/(a.win + a.lose) > (b.win/(b.lose + b.win)))) {
    return -1
  } else if ((a.win/(a.win + a.lose) < (b.win/(b.lose + b.win)))) {
    return 1
  } else {
    return 0
  }
}
const array = [{playerName: 'toto', win: 1, lose: 0}, {playerName: 'titi', win: 0, lose: 0}];
array.sort((a, b) => sortPlayers(a, b));



console.log(array)

The sort function must return an integer that defines the order of the items.

You're returning the result of the OR (||) operation instead.

Please check:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM