# Sort array alphabetically with a callback function

**URL:** <https://forum.freecodecamp.org/t/sort-array-alphabetically-with-a-callback-function/375846>\
**Category:** General\
**Created:** [April 19, 2020, 6:37pm UTC](https://forum.freecodecamp.org/t/sort-array-alphabetically-with-a-callback-function/375846 "2020-04-19T18:37:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmquazi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mmquazi/32/136469_2.png) [@mmquazi](https://forum.freecodecamp.org/u/mmquazi)\
**Post date:** [April 19, 2020, 6:37pm UTC](https://forum.freecodecamp.org/t/sort-array-alphabetically-with-a-callback-function/375846/1 "2020-04-19T18:37:12Z")

</div>

```auto
function reverseAlpha(arr) {
  return arr.sort(function(a, b) {
    return a === b ? 0 : a < b ? 1 : -1;
  });

```

In the function above, can someone please explain the part :  
return a === b ? 0 : a \< b ? 1 : -1;  
What exactly is happening here?

---

<div class="post-metadata">

**Author:** ![randomTask](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/randomtask/32/142384_2.png) [@randomTask](https://forum.freecodecamp.org/u/randomTask)\
**Post date:** [April 19, 2020, 7:01pm UTC](https://forum.freecodecamp.org/t/sort-array-alphabetically-with-a-callback-function/375846/2 "2020-04-19T19:01:58Z")

</div>

I believe the positive and negative value indicates the index being greater or less than in relation to the item being compared. I’m sure someone else on here can give a more thorough and even correct explanation…

Hopefully this resource can assist…

> **[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.

This section in particular:

_If `compareFunction` is not supplied, all non- `undefined` array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order. For example, “banana” comes before “cherry”. In a numeric sort, 9 comes before 80, but because numbers are converted to strings, “80” comes before “9” in the Unicode order. All `undefined` elements are sorted to the end of the array._

_If `compareFunction` is supplied, all non- `undefined` array elements are sorted according to the return value of the compare function (all `undefined` elements are sorted to the end of the array, with no call to `compareFunction` ). If `a` and `b` are two elements being compared, then:_

- If `compareFunction(a, b)` returns less than 0, sort `a` to an index lower than `b` (i.e. `a` comes first).
- If `compareFunction(a, b)` returns 0, leave `a` and `b` unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behavior, thus, not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
- If `compareFunction(a, b)` returns greater than 0, sort `b` to an index lower than `a` (i.e. `b` comes first).  
\*\* `compareFunction(a, b)` must always return the same value when given a specific pair of elements `a` and `b` as its two arguments. If inconsistent results are returned, then the sort order is undefined.

---

<div class="post-metadata">

**Author:** ![nikens](https://avatars.discourse-cdn.com/v4/letter/n/aeb1de/32.png) [@nikens](https://forum.freecodecamp.org/u/nikens)\
**Post date:** [April 19, 2020, 7:18pm UTC](https://forum.freecodecamp.org/t/sort-array-alphabetically-with-a-callback-function/375846/3 "2020-04-19T19:18:00Z")

</div>

What @randomTask says is correct. The documentation he provides is the interesting one, but those explanations can sometimes be a bit confusin. Let’s see if I can help a bit!

The `sort` function expects a function with two input arguments (which will be members of the sorted array) and a return value that can take 3 types of values: 0, greater than 0 (in the example they chose 1) and lower than 0 (in the example they chose 0).  
If the callback method given to `sort` returns

- 0: a and b have the same priority, so no need to change their position in the array
- 1: a should come after b in the array (the index of a will be greater than that of b)
- -1: a should come before b (the index of a will be lower than that of b)

Hope this help! 🙂

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:58pm UTC](https://forum.freecodecamp.org/t/sort-array-alphabetically-with-a-callback-function/375846/4 "2021-06-01T18:58:12Z")

</div>

Closed on June 1, 2021, 6:58pm UTC


