true if two strings are exactly equal, and
> operators compare strings in lexicographical order. Lexicographical order essentially means “dictionary order”. In simpler terms,
a < b if the first character of
a comes before the first character of
b in the ASCII table, or if the first characters of
b are equal and
a.slice(1) < b.slice(1).
Sorting an Array of Strings
Array#sort() function sorts by lexicographic order automatically.
sortBy() function also sorts by lexicographic order automatically, as long as you’re sorting an array of strings.
What about sorting an array of objects based on a string property? The
Array#sort() function takes a callback parameter that takes 2 values
b, and should return:
- A positive number if
a > b
- A negative number if
a < b
Given an array of objects with a
lastName property, below is one way to sort the array by
localeCompare() function. Calling
a < b,
a === b, and
a > b.