When we think of sorting arrays in JavaScript, we usually imagine strings, numbers, or even objects being organized in ascending or descending order. But what happens when the elements of the array are emojis? The answer might surprise you, especially when skin-tone modifiers come into play!
In this blog, weβll dive into how JavaScript handles sorting emojis, explore the Fitzpatrick scale that influences this behavior, and showcase some quirky examples along the way.
The Example
Letβs start with the code snippet from the screenshot:
["π©πΏ", "π©πΎ", "π©π½", "π©πΌ", "π©π»"].sort();
When executed, this code produces the following output:
["π©π»", "π©πΌ", "π©π½", "π©πΎ", "π©πΏ"]
At first glance, you might wonder, What just happened? The emojis were sorted in a "light-to-dark" order. To understand why this happens, we need to peek under the hood of JavaScript and Unicode.
Whatβs Really Happening?
1. How Emojis Work
Every emoji is represented by a unique Unicode code point. For example:
- π© is represented by
U+1F469
. - πΏ (a skin-tone modifier) is represented by
U+1F3FF
.
When an emoji like "π©πΏ" is displayed, it is actually a combination of two Unicode characters:
- The base emoji (
π©
). - A skin-tone modifier (
πΏ
).
2. Sorting in JavaScript
The .sort()
method in JavaScript works by comparing the Unicode values of array elements, treating them as strings by default. This means that the sorting order of emojis is determined by their underlying Unicode values.
3. Fitzpatrick Scale
The Fitzpatrick scale is a classification system for human skin tones, ranging from Type I (lightest) to Type VI (darkest). Unicode adopted this scale to introduce skin-tone diversity in emojis. Hereβs how the modifiers correspond to the Fitzpatrick scale:
- π» (Type I): Light
- πΌ (Type II): Light-Medium
- π½ (Type III): Medium
- πΎ (Type IV): Medium-Dark
- πΏ (Type V): Dark
When sorting emojis with these modifiers, JavaScript essentially orders them based on the numeric value of the skin-tone modifiers, which corresponds to the Fitzpatrick scale.
Letβs See This in Action
Hereβs a practical demonstration:
// Array of emojis with skin-tone modifiers
const emojis = ["π©πΏ", "π©πΎ", "π©π½", "π©πΌ", "π©π»"];
// Sort the array
const sortedEmojis = emojis.sort();
// Log the sorted array
console.log(sortedEmojis);
// Output: ["π©π»", "π©πΌ", "π©π½", "π©πΎ", "π©πΏ"]
Breaking It Down
- The
.sort()
method compares the Unicode values of each emoji. - The skin-tone modifiers (
π»
,πΌ
, etc.) have increasing Unicode values. - This results in the emojis being sorted from lightest (π») to darkest (πΏ).
Fun Experiments
Letβs try a few variations and observe the results.
1. Mixed Emojis
What happens if we include base emojis without skin-tone modifiers?
const mixedEmojis = ["π©πΏ", "π©πΎ", "π©", "π©π½", "π©πΌ", "π©π»"];
console.log(mixedEmojis.sort());
Output:
["π©", "π©π»", "π©πΌ", "π©π½", "π©πΎ", "π©πΏ"]
Here, the base emoji π©
comes first because it has the smallest Unicode value, followed by the sorted skin-tone variants.
2. Sorting Other Emoji Groups
What if we sort other emoji groups with modifiers, like hand gestures?
const handEmojis = ["ππΏ", "ππ½", "ππ»", "ππΌ", "ππΎ"];
console.log(handEmojis.sort());
Output:
["ππ»", "ππΌ", "ππ½", "ππΎ", "ππΏ"]
Once again, the emojis are sorted based on the Fitzpatrick scale.
Unicode Behind the Scenes
To understand the sorting behavior more deeply, letβs inspect the Unicode values of each emoji:
const emojiUnicode = emojis.map(e => e.codePointAt(0).toString(16));
console.log(emojiUnicode);
This will log the Unicode values of the base emoji and its modifiers. Youβll notice the incremental increase in values for the skin-tone modifiers.
Top comments (0)