# Sort an Array Alphabetically using the sort Method Error

**URL:** <https://forum.freecodecamp.org/t/sort-an-array-alphabetically-using-the-sort-method-error/312710>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [September 7, 2019, 10:23am UTC](https://forum.freecodecamp.org/t/sort-an-array-alphabetically-using-the-sort-method-error/312710 "2019-09-07T10:23:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [September 7, 2019, 10:23am UTC](https://forum.freecodecamp.org/t/sort-an-array-alphabetically-using-the-sort-method-error/312710/1 "2019-09-07T10:23:13Z")

</div>

**Tell us what’s happening:**  
I have done everything I should for the test, but the test fails. I have rewritten the code in many ways, and have even copy-pasted the ‘hint solution’. I have reset the code.

What is going on?

**Your code so far**

```js

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

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36`.

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![biscuitmanz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/biscuitmanz/32/188814_2.png) [@biscuitmanz](https://forum.freecodecamp.org/u/biscuitmanz)\
**Post date:** [September 7, 2019, 11:00am UTC](https://forum.freecodecamp.org/t/sort-an-array-alphabetically-using-the-sort-method-error/312710/2 "2019-09-07T11:00:14Z")

</div>

just using sort on its own sorts in alphabetical order no need for any arguments

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [September 7, 2019, 11:04am UTC](https://forum.freecodecamp.org/t/sort-an-array-alphabetically-using-the-sort-method-error/312710/3 "2019-09-07T11:04:28Z")

</div>

Right…Thanks.

I got caught up by the lesson saying it is best practice to always give a callback function.

---

<div class="post-metadata">

**Author:** ![biscuitmanz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/biscuitmanz/32/188814_2.png) [@biscuitmanz](https://forum.freecodecamp.org/u/biscuitmanz)\
**Post date:** [September 7, 2019, 11:20am UTC](https://forum.freecodecamp.org/t/sort-an-array-alphabetically-using-the-sort-method-error/312710/4 "2019-09-07T11:20:54Z")

</div>

```auto
return arr.sort(function(a,b) {
   if(a > b) {
     return 1
   }else{
     return -1
   }
  });

```

this is how you would do it with callback function or…

```auto
return arr.sort((a, b) => a > b ? 1 : -1);

```

---

<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 25, 2021, 12:16pm UTC](https://forum.freecodecamp.org/t/sort-an-array-alphabetically-using-the-sort-method-error/312710/5 "2021-01-25T12:16:54Z")

</div>

Closed on January 25, 2021, 12:16pm UTC


