---
title: "Discussion on \"JavaScript Array Sort Method and its gotchas - Explained with Examples\" | Hashnode"
description: "Discussion on \"JavaScript Array Sort Method and its gotchas - Explained with Examples\". In this article, we will see how to properly use the array sort method and learn common gotchas associated with it.\n\nWant to learn Redux in detail and build a complete food ordering app? check out my Mastering Redux course. Following is the preview o..."
url: "https://hashnode.com/posts/javascript-array-sort-method-and-its-gotchas-explained-with-examples/60558c965f8d512169726440"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [JavaScript Array Sort Method and its gotchas - Explained with Examples](https://blog.yogeshchavan.dev/javascript-array-sort-method-and-its-gotchas-explained-with-examples?utm_source=hashnode&utm_medium=feed)

In this article, we will see how to properly use the array sort method and learn common gotchas associated with it. Want to learn Redux in detail and build a complete food ordering app? check out my Mastering Redux course. Following is the preview o...

Mar 20, 2021 · 5 min read · blog.yogeshchavan.dev  
Updated Jun 16, 2022  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#reactjs](https://hashnode.com/tag/reactjs)
-   [#codenewbies](https://hashnode.com/tag/codenewbies)

## Written by

[Yogesh Chavan](https://hashnode.com/@myogeshchavan97)

@myogeshchavan97 · India

  
[View profile](https://hashnode.com/@myogeshchavan97)

Full Stack Developer | Freelancer | JavaScript | React | Nodejs. https://yogeshchavan.dev/. Book 1:1 @ hiretheauthor.com/myogeshchavan97

Full Stack Developer | Freelancer | JavaScript | React | Nodejs. Loves sharing knowledge through technical articles. Dev.to: https://dev.to/myogeshchavan97 https://linktr.ee/myogeshchavan97

## Responses 1

Markdown supported · ⌘↵ to post

[Usman maaji zaharadeen](https://hashnode.com/@Maaji)

[Mar 21, 2021](https://hashnode.com/posts/javascript-array-sort-method-and-its-gotchas-explained-with-examples/60558c965f8d512169726440/comment/60570b295f8d512169727918)

very interesting blog thanks for this. in ur array to object sort() comparism why using alot of return statement instead of one, one works just fyn for me, is there anything special about returning zero(0)

> const names\_of\_employees = \[ {name:'ahmad', age:30},{name: 'maaji', age: 21}, {name:'musa', age:16}, {name:'bulldozer', age:70} \]
> 
> let sort\_names = names\_of\_employees.slice().sort(function (user1, user2){ if(user1.age < user2.age) return -1; return 0; _why returning this_ })

[Yogesh Chavan](https://hashnode.com/@myogeshchavan97)

[Mar 21, 2021](https://hashnode.com/posts/javascript-array-sort-method-and-its-gotchas-explained-with-examples/60558c965f8d512169726440/comment/60570b295f8d512169727918#reply-60570c5d63a05a21443f2dbf)

Thanks [deeni buba](https://hashnode.com/@Buba). If the age of two objects are equal then both the If conditions will be false and therefore the last `return 0` statement is added.

[Usman maaji zaharadeen](https://hashnode.com/@Maaji)

[Mar 21, 2021](https://hashnode.com/posts/javascript-array-sort-method-and-its-gotchas-explained-with-examples/60558c965f8d512169726440/comment/60570b295f8d512169727918#reply-60578ef863a05a21443f3490)

[Yogesh Chavan](https://hashnode.com/@myogeshchavan97) my mistake I didn't realize the logic, if both failed something is there to return. I understand thanks alot 🙌 for this
