# Sorting Arrays (Comparator Function)

**URL:** <https://forum.codewithmosh.com/t/sorting-arrays-comparator-function/1161>\
**Category:** JavaScript\
**Created:** [December 6, 2020, 10:41pm UTC](https://forum.codewithmosh.com/t/sorting-arrays-comparator-function/1161 "2020-12-06T22:41:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Orange](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/orange/32/376_2.png) [@Orange](https://forum.codewithmosh.com/u/Orange)\
**Post date:** [December 6, 2020, 10:41pm UTC](https://forum.codewithmosh.com/t/sorting-arrays-comparator-function/1161/1 "2020-12-06T22:41:46Z")

</div>

Hello All,  
Why are these two codes giving different outputs when using the comparator function? Is it based on the unicode value of the first letters(‘N’ and ‘J’)or the length of the string(amount of characters)?

const numeric = [  
{id:1, name: “Node.js”},  
{id:2, name: “Javascript”}  
];

- Mosh Method:  
numeric.sort(function(a,b) {  
if(a.name \< b.name) return -1;  
if(a.name \> b.name) return 1;  
return 0;  
});  
console.log(numeric);  
//Output:  
1: {id: 2, name: “Javascript”}  
0: {id: 1, name: “Node.js”}

- Other Method:  
const sortedObj = function(a,b) {  
return a.name - b.name;  
}  
console.log(numeric.sort(sortedObj));  
//Output:  
0: {id: 1, name: “Node.js”}  
1: {id: 2, name: “Javascript”}

---

<div class="post-metadata">

**Author:** ![A100D-JS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/a100d-js/32/8_2.png) [@A100D-JS](https://forum.codewithmosh.com/u/A100D-JS)\
**Post date:** [December 7, 2020, 1:22am UTC](https://forum.codewithmosh.com/t/sorting-arrays-comparator-function/1161/2 "2020-12-07T01:22:41Z")

</div>

Hi,

> **[Array.prototype.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)**
>
> The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

 ![Sort Parameters Functionality 1](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/1X/e83b94d8ecd48cf8ee13516730cf9e4aaa9b9c33.png)

 ![Sort Parameters Functionality 2](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/1X/0c818da9a3e5382af9eccc347553cbe3563f6671.png)

 ![Sort Parameters Functionality 3](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/1X/3469fac476b13c938b3fab6dc2509d2d7fe2016b.png)

[https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html](https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html)

The explanation of the first link is clear, **and about the ascii table :** the decimal number of each character is different so if you’re getting different result it’s because of the decimal number and the condition you have set in your code.

---

<div class="post-metadata">

**Author:** ![Orange](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/orange/32/376_2.png) [@Orange](https://forum.codewithmosh.com/u/Orange)\
**Post date:** [December 7, 2020, 6:37pm UTC](https://forum.codewithmosh.com/t/sorting-arrays-comparator-function/1161/3 "2020-12-07T18:37:41Z")

</div>

Hi thank you for the attachments. But I still need clarification on the how/why the second code is executed.

I understand Mosh’s method and how comparator function is applied, and that the result is ascending(least =\> greatest); based on the ASCII Table you linked ‘N’ = 78 and ‘J’ = 74 so:

- function(a,b){ return 78 - 74}

- // 78 - 74 = 4(positive value) so a\>b returns 1 meaning b comes ahead of a

However, the second code should give the same result,

---

<div class="post-metadata">

**Author:** ![A100D-JS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/a100d-js/32/8_2.png) [@A100D-JS](https://forum.codewithmosh.com/u/A100D-JS)\
**Post date:** [December 8, 2020, 1:30am UTC](https://forum.codewithmosh.com/t/sorting-arrays-comparator-function/1161/4 "2020-12-08T01:30:00Z")

</div>

Yes you’re right, i tried everything with your code but nothing worked.  
But i think the reason your code isn’t working is because sorting **strings** is different from **numbers** , and as document mentioned :

**To compare numbers instead of strings, the compare function can simply subtract `b` from `a`**

```auto
`function compareNumbers(a, b) {

  return a - b;

}

```

 ![Strings Vs Numbers](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/1X/840db979852b8698e26ffa71c4b94a19d479fe27.png)

and if you look at the code above which is from document you’ll notice that it sorted **numbers(values)** differently from **strings(names)**.

I tried this and it worked :

```auto
    `const numeric = [

        { id: 1, name: "Nodejs" },

        { id: 2, name: "Javascript" }

    ];

    const sortedObj = function (a, b) {

        if (a.name < b.name) return -1;

        if (a.name > b.name) return 1;

        return 0;

    }

    console.log(numeric.sort(sortedObj));

//OUtPUT:
      1. 0: {id: 2, name: "Javascript"}
      2. 1: {id: 1, name: "Nodejs"}
      3. length: 2
      4. __proto__ : Array(0)

```

But i’m not completely sure if that is the only reason.

Maybe @SAM could help you better with your problem.

---

<div class="post-metadata">

**Author:** ![Orange](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/orange/32/376_2.png) [@Orange](https://forum.codewithmosh.com/u/Orange)\
**Post date:** [December 8, 2020, 2:47am UTC](https://forum.codewithmosh.com/t/sorting-arrays-comparator-function/1161/5 "2020-12-08T02:47:54Z")

</div>

Thank you for taking the time, that makes more sense. I simply thought I could try the shorter syntax, since it was cleaner, but I believe you are right: sorting value vs. string are different. The Mosh tutorial video only showed sort by string, so i assumed this applies to numbers by default.

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [December 8, 2020, 6:05am UTC](https://forum.codewithmosh.com/t/sorting-arrays-comparator-function/1161/6 "2020-12-08T06:05:11Z")

</div>

The second method doesn’t work because subtracting a string from another doesn’t give a sensible numeric result. How much is “Node.js” minus “Javascript”? It’s `NaN` .

---

<div class="post-metadata">

**Author:** ![Orange](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/orange/32/376_2.png) [@Orange](https://forum.codewithmosh.com/u/Orange)\
**Post date:** [December 8, 2020, 7:14pm UTC](https://forum.codewithmosh.com/t/sorting-arrays-comparator-function/1161/7 "2020-12-08T19:14:09Z")

</div>

Thanks for the help @SAM, and @A100D-JS.
