---
title: "Discussion on \"5 useful tips about the JavaScript array sort method\" | Hashnode"
description: "Discussion on \"5 useful tips about the JavaScript array sort method\". JavaScript arrays are an ordered collection that can hold data of any type. Arrays are created with square brackets [...] and allow duplicate elements. In JavaScript, we can sort the elements of an array with the built-in method called, sort().\nIn th..."
url: "https://hashnode.com/posts/5-useful-tips-about-the-javascript-array-sort-method/5f76f70c9c3b6e410121591e"
---

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

Discussion

# [5 useful tips about the JavaScript array sort method](https://blog.greenroots.info/5-useful-tips-about-the-javascript-array-sort-method?utm_source=hashnode&utm_medium=feed)

JavaScript arrays are an ordered collection that can hold data of any type. Arrays are created with square brackets \[...\] and allow duplicate elements. In JavaScript, we can sort the elements of an array with the built-in method called, sort(). In th...

Oct 2, 2020 · 11 min read · blog.greenroots.info  
Updated Feb 2, 2021  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#programming](https://hashnode.com/tag/programming)
-   [#beginner](https://hashnode.com/tag/beginner)
-   [#data-structures](https://hashnode.com/tag/data-structures)

## Written by

[Tapas Adhikary](https://hashnode.com/@atapas)

@atapas · Bangalore

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

Educator @tapaScript | Founder CreoWis & ReactPlay - Writer - YouTuber - Open Source

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human

## Responses 3

Markdown supported · ⌘↵ to post

[Christopher Norris](https://hashnode.com/@afrpar)

[Oct 24, 2022](https://hashnode.com/posts/5-useful-tips-about-the-javascript-array-sort-method/5f76f70c9c3b6e410121591e/comment/63561004499ee6e7154b7b57)

This is wonderful and helpful information.

[Darlan Tódero ten Caten](https://hashnode.com/@darlantc)

[Oct 2, 2020](https://hashnode.com/posts/5-useful-tips-about-the-javascript-array-sort-method/5f76f70c9c3b6e410121591e/comment/5f7781a97d160d41227d6ea6)

Very well explained, thank you!

To contribute here is a compare function I use to sort objects by an property name:

```
export const dynamicSortByProperty = (property: string, asc = true) => {
  const sortOrder = asc ? 1 : -1;
  return (a, b) => {
    if (!a.hasOwnProperty(property) || !b.hasOwnProperty(property)) {
      // property doesn't exist on either object
      return 0;
    }

    const varA =
      typeof a[property] === "string" ? a[property].toUpperCase() : a[property];
    const varB =
      typeof b[property] === "string" ? b[property].toUpperCase() : b[property];

    const result = varA < varB ? -1 : varA > varB ? 1 : 0;
    return result * sortOrder;
  };
};
```

[Tapas Adhikary](https://hashnode.com/@atapas)

[Oct 3, 2020](https://hashnode.com/posts/5-useful-tips-about-the-javascript-array-sort-method/5f76f70c9c3b6e410121591e/comment/5f7781a97d160d41227d6ea6#reply-5f77ed997d160d41227d7139)

[Darlan Todero ten Caten](https://hashnode.com/@darlantc), Thank you very much for adding to it. Awesome!

[Subha Chanda](https://hashnode.com/@nemo)

[Oct 2, 2020](https://hashnode.com/posts/5-useful-tips-about-the-javascript-array-sort-method/5f76f70c9c3b6e410121591e/comment/5f76fbea9c3b6e4101215984)

Sort method is one of my favourite array methods. Thank you for the write-up. 😊

[Tapas Adhikary](https://hashnode.com/@atapas)

[Oct 2, 2020](https://hashnode.com/posts/5-useful-tips-about-the-javascript-array-sort-method/5f76f70c9c3b6e410121591e/comment/5f76fbea9c3b6e4101215984#reply-5f76fc8b7d160d41227d67f1)

Good to know! Yes, mine too.. In fact all the array methods are fun 😊
