---
title: "Discussion on \"Sorting in JavaScript\" | Hashnode"
description: "Discussion on \"Sorting in JavaScript\". Typical use-cases and rare edge-cases"
url: "https://hashnode.com/posts/sorting-in-javascript/61e525c98a425719c1735ae4"
---

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

Discussion

# [Sorting in JavaScript](https://blog.rupertmckay.com/sorting-in-javascript?utm_source=hashnode&utm_medium=feed)

Typical use-cases and rare edge-cases

Jan 17, 2022 · 8 min read · blog.rupertmckay.com  
Updated Jan 18, 2022  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#sorting](https://hashnode.com/tag/sorting)
-   [#computer-science](https://hashnode.com/tag/computer-science)

## Written by

[Rupert McKay](https://hashnode.com/@fildon)

@fildon · Delft, The Netherlands

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

Staff Software Engineer at Lokalise

## Responses 2

Markdown supported · ⌘↵ to post

[Marcelo Zárate](https://hashnode.com/@marcelozarate)

[Jan 19, 2022](https://hashnode.com/posts/sorting-in-javascript/61e525c98a425719c1735ae4/comment/61e7f1336f4d6f431978d071)

Wow, I really love your writing! Glad you shared those edgy cases, I hope it stays in my mind. Awesome article.

[Rupert McKay](https://hashnode.com/@fildon)

[Jan 19, 2022](https://hashnode.com/posts/sorting-in-javascript/61e525c98a425719c1735ae4/comment/61e7f1336f4d6f431978d071#reply-61e7f2f50204273bdcb10e1e)

Thank you, Marcelo!

It's comments like these that make me excited to keep writing articles in the future.

[Evan Brodie](https://hashnode.com/@ecbrodie)

[Jan 18, 2022](https://hashnode.com/posts/sorting-in-javascript/61e525c98a425719c1735ae4/comment/61e6d1bf0204273bdcb0f3c6)

Great article, but you skipped over what I believe is the biggest quirk of `Array.prototype.sort()`: _sorting is applied in-place on the array, ie it mutates the array!_ This is not immediately apparent if you were to do something like `const sortedArray = unsortedArray.sort()`, that you just changed the `unsortedArray` too. I have worked on several teams who have been burned by hard-to-find bugs caused by this behaviour, which is not easily caught by a linter. I'd advise you mention this in your article too.

[Rupert McKay](https://hashnode.com/@fildon)

[Jan 18, 2022](https://hashnode.com/posts/sorting-in-javascript/61e525c98a425719c1735ae4/comment/61e6d1bf0204273bdcb0f3c6#reply-61e6da050204273bdcb0f471)

That's a great point! I'll update the article to make that clear.

EDIT: I have made an update now and called out the in-place sorting in a few parts of the post.

Thanks again for pointing this out!
