A[1] > b[1], comparing strings not clear

A[1] > b[1], comparing strings not clear
0

#1

Hi to all, I have a problem with string comparing here. One simple line is not clear to me.

return Object.keys(inventory).map(function(value) {
      return [inventory[value], value];
    }).sort(function(a, b) {
      if(a[1] === b[1]) return 0;
      else return (a[1] > b[1]) ? 1 : -1;
    });

a[1] > b[1]

I know it’s comparing two strings from objects but how does this work? I’ve been searching about this but there’s no answer.
I’ve found this on MDN but it still doesn’t give me an answer. What do <, > mean when comparing two strings?

Thanks in advance!
PS If you need full code, I’ll provide it to you.


#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

The same thing they would mean if comparing to primitive strings.

'hello'  < 'goodbye' // false
'apple' < 'banana' // true

#4

Let’s say I have the following inventory and want to sort it by the 2nd element of each sub array of inventory. I could do that with the following:

var inventory = [
  [ 5, 'Microphone' ],  
  [ 3, 'Hair Pin' ],  
  [ 88, 'Bowling Ball' ],
  [ 2, 'Dirty Sock' ],
  [ 7, 'Toothpaste' ], 
  [ 3, 'Half-Eaten Apple' ]
];

inventory.sort(function (a, b) {
  if (a[1] > b[1]) {
   return 1;
  }
  if (a[1] < b[1]) {
    return -1;
  }
  return 0;
});

console.log(inventory);

The above would display the following in the console, which is what I wanted.

[ [ 88, ‘Bowling Ball’ ],
[ 2, ‘Dirty Sock’ ],
[ 3, ‘Hair Pin’ ],
[ 3, ‘Half-Eaten Apple’ ],
[ 5, ‘Microphone’ ],
[ 7, ‘Toothpaste’ ] ]

What if I want to see what happening to the inventory array at each step of the sort function? I could rewrite the sort callback function with some console.log statements to see how inventory is rearranged during/after each iteration of the sort.

var inventory = [
  [ 5, 'Microphone' ],  
  [ 3, 'Hair Pin' ],  
  [ 88, 'Bowling Ball' ],
  [ 2, 'Dirty Sock' ],
  [ 7, 'Toothpaste' ], 
  [ 3, 'Half-Eaten Apple' ]
];

var iterationNum = 1;
inventory.sort(function (a, b) {
  console.log();  
  console.log('iteration #'+iterationNum++);
  console.log('inventory before comparing a[1] ('+a[1]+ ') and b[1] ('+b[1] +')');
  console.log(inventory);
  if (a[1] > b[1]) {
  console.log('return 1 so '+ JSON.stringify(a)  +' is located after ' + JSON.stringify(b));
  return 1;
  }
  if (a[1] < b[1]) {
    console.log('return -1 so '+ JSON.stringify(b)  +' is located after ' + JSON.stringify(a));
    return -1;
  }
  console.log('return 0 so nothing gets changed');
  return 0;
});
console.log();
console.log('final inventory')
console.log(inventory);

See the following repl.it to see above code in action.


#5

It all makes sense now! Thank you very much!