How to sort the array in correct order to match all test cases

Tell us what’s happening:
i have tried both chngArr[].sort((a,b)=> a[1] > b[1]) and chngArr[].sort((a,b)=> a[1] < b[1]). but both cases are sorting chngArr into desired one. it’s giving the same output in both cases.

Your code so far


function checkCashRegister(price, cash, cid) {
  // Here is your change, ma'am.
  let currarr = ['Penny','Nickel','Dime','Quarter','Dollar','Five Dollars','Ten Dollars','Twenty Dollars','One-hundred Dollars'];
  let currVal =[0.01,0.05,0.1,0.25,1,5,10,20,100];
  let change = cash - price; // change to provide
  let change1 = change;
  let chngArr = []; //array to store change values
  let paidCash = 0;
  let totalCash = 0;
  for(let i = cid.length-1;i>=0; i--){
    let e = cid[i];
    totalCash += e[1];
    if(currVal[i] > change && e[1] > 0){
      continue;
    }
    else {
      if(paidCash < change1 && change > 0 ){
        let t = Math.floor(change/currVal[i]);
        let currchg = t*currVal[i] < e[1] ? t*currVal[i] : e[1];
        chngArr.push([e[0], currchg]);
        change -= currchg;
        paidCash += currchg;
        paidCash = parseFloat(paidCash.toFixed(2)); // convert to 2 decimal places
        change = parseFloat(change.toFixed(2));
      }
    }
    // console.log(paidCash, change);
  }
  chngArr.sort((a,b)=> a[1] < b[1]); 
  // console.log(chngArr);
    console.log(change1,paidCash, totalCash);
  if(change1 > paidCash ){
    return {status: "INSUFFICIENT_FUNDS", change: []}
  }
  else if(paidCash === change1 && totalCash > change1){
    return {status: "OPEN", change: chngArr};
  }
  else if(totalCash === change1){
    return {status: "CLOSED", change: chngArr};
  }
  else{
    return { status:'OPEN', change: chngArr};

  }
  // console.log(currarr,currVal);
}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55]
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]
let results = checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
console.log(results.change, results.status);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/

Check out the documentation for Array.prototype.sort. Your comparator function needs to return a number value, not a boolean. Try a[1] - b[1].

thanks @PortableStick. I have to use some extra code to last test case.