I have encountered this javascript code involving a sort method with a custom return.
const nums = ['9', '5', '3', '34', '30' ];
const num = nums.map(n => n.toString()).sort((a,b) => a + b < b + a? 1: -1).join('')
Essentially, this code is returning the largest possible integer. I have the basic knowledge of sort, if you want the numbers in ascending order then you'll use a-b for the return. If descending is desired, then you would use b-a.
I want to know how the a and b are working behind the scenes. I couldn't find a source where it explains in details how the sort works. I would like to know step by step when the sort is in action; it will give me a better a idea how the code above works.
Your help will be appreciated.
-1a + bis string concatenation not arithmetical operation and the sort function is ordering the elements alphabetically not numerically (111will be before2)