11.Sorting and Reversing the Arrays In JavaScript -reverse() - sort() - Compare Function working

preview_player
Показать описание
The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
he reverse method transposes the elements of the calling array object in place, mutating the array, and returning a reference to the array.

reverse is intentionally generic; this method can be called or applied to objects resembling arrays. Objects which do not contain a length property reflecting the last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner.

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

The time and space complexity of the sort cannot be guaranteed as it depends on the implementation.

pecifies a function that defines the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.
If compareFunction is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order. For example, "banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in the Unicode order. All undefined elements are sorted to the end of the array

If compareFunction(a, b) is less than 0, sort a to an index lower than b (i.e. a comes first).
If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behavior, thus, not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
If compareFunction(a, b) is greater than 0, sort b to an index lower than a (i.e. b comes first).
compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned, then the sort order is undefined.

#Sorting #Reversing #Compare_Function
11.Sorting and Reversing the Arrays In JavaScript -reverse() - sort() - Compare Function working
Рекомендации по теме
Комментарии
Автор

I watch many videos, I get confused about the sort method, but I fully understand it now thanks for helping my life.

oghenefokethompson
Автор

After doing too much research on the inner logic of the compare function on youtube, finally got cleared my concept from here.Great content 👍🏻❤️

usman_ranaa