Why sorting is not correct?

To be sorted by width, but oddly sorts, what's the problem?
obj.sort((a, b) => {
 console.log(a.the width b.width);
 return a.width > b.width ? 1 :-1

April 3rd 20 at 18:28
1 answer
April 3rd 20 at 18:30
Line also are compared character by character. Subtract them, will be automatically converted to numbers:

return a.width - b.width

And it would be better of course, and even to deal with the data source and retrieve numeric values as numbers, not strings.
left unchanged, the type of the moved from string to number.
Thank you - Lucinda.Windler commented on April 3rd 20 at 18:33

Find more questions by tags JavaScript