Skip to content

Commit

Permalink
Add custom sorter to ignore HTML and capitalization (fixes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpost committed Feb 8, 2020
1 parent 65c8fb3 commit 3280fe6
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/sorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import { dispatch } from '@wordpress/data';
*/
import { SortIcon } from './icons';

const removeHtmlTags = ( string ) => {
const div = document.createElement( 'div' );
div.innerHTML = string;

return div.innerText;
};

const sortListItems = ( items, reverse = false ) => {
const parser = new DOMParser();
const list = parser
Expand Down Expand Up @@ -46,7 +53,12 @@ const sortListItems = ( items, reverse = false ) => {

return li.innerHTML;
} )
.sort();
.sort( ( a, b ) =>
removeHtmlTags( a ).toUpperCase() <
removeHtmlTags( b ).toUpperCase()
? -1
: 1,
);

if ( reverse ) {
return sortedItems.reverse();
Expand Down

0 comments on commit 3280fe6

Please sign in to comment.