JS Coding Questions Logo
JS Coding Questions
#323๐Ÿ’ผ Interview๐Ÿ’ป Code

What is collation

Advertisement

728x90

Collation is used for sorting a set of strings and searching within a set of strings. It is parameterized by locale and aware of Unicode. Let's take comparison and sorting features,

1. Comparison:

javascript
1var list = ["รค", "a", "z"]; // In German,  "รค" sorts with "a" Whereas in Swedish, "รค" sorts after "z"
2
3var l10nDE = new Intl.Collator("de");
4
5var l10nSV = new Intl.Collator("sv");
6
7console.log(l10nDE.compare("รค", "z") === -1); // true
8
9console.log(l10nSV.compare("รค", "z") === +1); // true

2. Sorting:

javascript
1var list = ["รค", "a", "z"]; // In German,  "รค" sorts with "a" Whereas in Swedish, "รค" sorts after "z"
2
3var l10nDE = new Intl.Collator("de");
4
5var l10nSV = new Intl.Collator("sv");
6
7console.log(list.sort(l10nDE.compare)); // [ "a", "รค", "z" ]
8
9console.log(list.sort(l10nSV.compare)); // [ "a", "z", "รค" ]

Advertisement

Responsive Ad
๐ŸŽฏ Practice NowRelated Challenge

JavaScript Coding Exercise 66

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
323of476