# Sort an Array using sort(): wrong solution provided

**URL:** <https://forum.freecodecamp.org/t/sort-an-array-using-sort-wrong-solution-provided/272352>\
**Category:** freeCodeCamp Contributors\
**Tags:** contributing\
**Created:** [April 18, 2019, 5:37pm UTC](https://forum.freecodecamp.org/t/sort-an-array-using-sort-wrong-solution-provided/272352 "2019-04-18T17:37:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AleksaStevic](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@AleksaStevic](https://forum.freecodecamp.org/u/AleksaStevic)\
**Post date:** [April 18, 2019, 5:37pm UTC](https://forum.freecodecamp.org/t/sort-an-array-using-sort-wrong-solution-provided/272352/1 "2019-04-18T17:37:34Z")

</div>

## Functional Programming: Sort an Array Alphabetically using the sort Method

The solution provided doesn’t work:

```auto
function alphabeticalOrder(arr) {
  // Add your code below this line
  return arr.sort(function(a,b) {
    return a > b;
  });
  // Add your code above this line
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);

```

This solution works:

```auto
function alphabeticalOrder(arr) {
  // Add your code below this line
  return arr.sort(function(a,b) {
    if (a > b) return 1;
    if (a == b) return 0;
    if (a < b) return -1;
  });
  // Add your code above this line
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);

```

Reason (from MDN web docs):  
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)` is 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 behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
- If `compareFunction(a, b)` is 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:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [April 18, 2019, 5:45pm UTC](https://forum.freecodecamp.org/t/sort-an-array-using-sort-wrong-solution-provided/272352/2 "2019-04-18T17:45:41Z")

</div>

Thank you for helping make FCC better. Bugs can be reported as [GitHub Issues](https://github.com/freeCodeCamp/freeCodeCamp/issues). Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

Updating guide articles (which is what the hints and suggested solutions are) is actually extremely easy and requires almost no programming knowledge! Check it out and get that first PR under your belt!

---

<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:** [January 17, 2021, 7:59pm UTC](https://forum.freecodecamp.org/t/sort-an-array-using-sort-wrong-solution-provided/272352/4 "2021-01-17T19:59:02Z")

</div>

Closed on January 17, 2021, 7:59pm UTC


