<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Paul Springett on Medium]]></title>
        <description><![CDATA[Stories by Paul Springett on Medium]]></description>
        <link>https://medium.com/@paulspringett?source=rss-cece4427542a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4eFX3xssUd752GEuVtcCKA.jpeg</url>
            <title>Stories by Paul Springett on Medium</title>
            <link>https://medium.com/@paulspringett?source=rss-cece4427542a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 23:03:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@paulspringett/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Searching and sorting text with diacritical marks in JavaScript]]></title>
            <link>https://medium.com/thread-engineering/searching-and-sorting-text-with-diacritical-marks-in-javascript-45afef20e7f2?source=rss-cece4427542a------2</link>
            <guid isPermaLink="false">https://medium.com/p/45afef20e7f2</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[unicode]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Paul Springett]]></dc:creator>
            <pubDate>Wed, 29 Aug 2018 07:14:32 GMT</pubDate>
            <atom:updated>2018-08-30T10:34:34.377Z</atom:updated>
            <content:encoded><![CDATA[<p>We’ve been busy rebuilding Thread’s frontend codebase using <a href="https://reactjs.org/">React</a>, and lately that’s included rebuilding our <a href="https://www.thread.com/browse">browse filters</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4bw_W-r9V6oKQWZvCa5ysQ.png" /><figcaption>Searchable browse filters on Thread</figcaption></figure><p>These filters allow customers to find products more easily by selecting which brands, colours or prices they want to see.</p><p>Because some of our filters contain a lot of options (we have over 500 brands available on Thread) we need them to be easily searchable, and sorted correctly.</p><h3>Sorting</h3><p>Take the following list of brands:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2005dab3946f7eb0ce6493ca80900d05/href">https://medium.com/media/2005dab3946f7eb0ce6493ca80900d05/href</a></iframe><p>Let’s see how we can best sort them intuitively for our users.</p><p>Sorting these in JavaScript initially seemed straightforward: just call sort() on the brands array, passing a function to compare the two labels, eg:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8845adb798939f440804e3b0b3420a7/href">https://medium.com/media/a8845adb798939f440804e3b0b3420a7/href</a></iframe><p>This appears to work, with the exception of Études, which gets incorrectly placed at the bottom of the list. JavaScript compares strings lexicographically, but with limitations. It actually uses the character’s position in the Unicode table to determine its ordering.</p><p>If we look the characters <a href="https://unicode-table.com/en/007A/">z</a> and <a href="https://unicode-table.com/en/00E9/">é</a> we can see the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e59d590eefbdbf39b8ed20a3c230831/href">https://medium.com/media/5e59d590eefbdbf39b8ed20a3c230831/href</a></iframe><p>Notice the decimal values for the characters; é has a much higher value than z meaning it will be sorted after z.</p><p>Fortunately, JavaScript has <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare">String.prototype.localeCompare</a> which meaning we can simply rewrite the above function to be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/087c4db8e8eed3dc9fb5a24e659bce85/href">https://medium.com/media/087c4db8e8eed3dc9fb5a24e659bce85/href</a></iframe><p>Have a read <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare">of the docs</a> to see what other arguments the localeCompare function accepts.</p><h3>Searching</h3><p>Sorting was quite straightforward to solve, but what about searching?</p><p>Our browse filters provide a handy search input that filters large lists as you type, however searching for Cote was not yielding results for Côte et Ciel.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f846031573fa2433a62e67ffa01569c/href">https://medium.com/media/9f846031573fa2433a62e67ffa01569c/href</a></iframe><p>We didn’t think it was reasonable to expect our customers to know we’re using the brand spelling with diacritics, or to be able to easily input the right characters.</p><p>So in order to allow customers to find these brands, we need to remove the diacritical marks from the characters before comparing them with the search query.</p><p>We can do this in two parts.</p><p>First, we need to “decompose” the string so that any characters with diacritical marks are represented by their two-byte surrogate pair. This is a technique used in UTF-8 to represent a single character as a pair of two bytes.</p><p>We can do this using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize">normalize function</a> in JavaScript, and passing the NFD form.</p><p>Looking at the docs, we can see that “NFD” stands for “Normalization Form Canonical Decomposition”–the important word being “Decomposition”.</p><p>We can see this working by comparing the string length before and after decomposition:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b7694f3abcbb4163ccd5e96d61b7971/href">https://medium.com/media/7b7694f3abcbb4163ccd5e96d61b7971/href</a></iframe><p>This shows that the ô character is now being represented by two bytes rather than one.</p><p>Second, now we have separate bytes for the character and the diacritical mark, we can remove the unwanted characters using the String.prototype.replace function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/735c2697eca4b1f5405a13df2fc337d0/href">https://medium.com/media/735c2697eca4b1f5405a13df2fc337d0/href</a></iframe><p>The replace function takes a range of Unicode points from <a href="https://unicode-table.com/en/#0300">U+0300</a> to <a href="https://unicode-table.com/en/#036F">U+036F</a>, covering any diacritic bytes we might have in the string.</p><p>Et voilà! This is a great technique to have in mind if you’re allowing your users to search lists of data that might include diacritics.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=45afef20e7f2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/thread-engineering/searching-and-sorting-text-with-diacritical-marks-in-javascript-45afef20e7f2">Searching and sorting text with diacritical marks in JavaScript</a> was originally published in <a href="https://medium.com/thread-engineering">Thread Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>