JavaScript Array.sort with examples

I was reading a social post and found that Array.sort() works great across all the browser. I thought to write small demo to show powerful compare function to sort object type array on given property.

Example: Array.sort()

Also, Array.sort() is a string sort by default! Should be used for string like type only!

array-sort array-sort-output

So, you can not use for Array.sort() for numeric sort! 🙁

But, you can use compare callback function to work with Object type Array very efficiently.

Example: Array.sort(compareFunction)

array-sort-compare-function array-sort-compare-function-output

Read: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort?redirect=no

One of the best way to check property of an Object in JavaScript

I always wonder which is the best way to check property of any Object in Javascript. Here is the example

Example 1

Example 2

Bonsai

I did not know about Bonsai till few months back. Charmy introduced me to Bonsai. Bonsai is a Japanese art form using miniature trees grown in containers. It is miniature version of Trees, grows in small container. I was very much impressed by the idea. I thought that I should share with you all as well.