Thursday 29 January 2015

Second set of eyes on some code, pls?

G'day:
I'm having some trouble working out if I'm doing something wrong, or whether there's a bug in the library I'm using here. I'm erring towards it being me being daft and missing something. Could you perhaps cast yer eyes over this lot and see if I'm being a muppet? Cheers.

I'm trying to use the TinySort library to sort words that have characters with diacritic marks, eg: àáâãäåæ. TinySort has this capability built-in ("non-latin characters"), but I'm buggered if I can get it to work.

Here's a pared-back repro of the issue:

<!doctype html>
<!-- min.html -->
<html lang="en-IE">
<head>
    <meta charset="utf-8">
    <title>Test tinysort</title>
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tinysort/2.1.1/tinysort.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tinysort/2.1.1/tinysort.charorder.js"></script>
</head>
<body>


Current sort: <span id="currentSort">ID</span><br>
Current charOrder subset: <span id="charOrder">a[àáâãäåæ]</span><br>


<ol id="sortMe">
    <li data-id="1">ab</li><li data-id="2">Ac</li><li data-id="3">Àd</li><li data-id="4">Áe</li><li data-id="5">Âf</li><li data-id="6">Ãg</li><li data-id="7">Äh</li><li data-id="8">Åi</li><li data-id="9">Æj</li><li data-id="10">àk</li><li data-id="11">ál</li><li data-id="12">âm</li><li data-id="13">ãn</li><li data-id="14">äo</li><li data-id="15">åp</li><li data-id="16">æq</li>

    <li data-id="17">cr</li><li data-id="18">Cs</li><li data-id="19">Çt</li><li data-id="20">çu</li>
</ol>


<form action="." method="get">
    <button id="sortByLabel" data-dir="asc">Sort</button>
    <button id="sortRest">Reset</button>
    <br><br>
    charOrder subset to use:<br>
    <input type="radio" name="charOrder" value="a[àáâãäåæ]" checked>A<br>
    <input type="radio" name="charOrder" value="a[àáâãäåæ]c[ç]">A &amp; C<br>
    <input type="radio" name="charOrder" value="a[àáâãäåæ]c[ç]e[èéêë]i[ìíîï]n[ñ]o[òóôõöø]s[š]u[ùúûü]y[ÿý]">All<br>
</form>
<script src="./sortHandling.js"></script>
</body>
</html>


// sortHandling.js

$(document).ready(function(){
    var charOrderRules = "a[àáâãäåæ]";

    $("#sortByLabel").on("click", function(e){
        var $this = $(this);
        var dir = $this.attr("data-dir");

        tinysort("#sortMe > li", {order:dir, charOrder:charOrderRules});

        var newDir = dir === "asc" ? "desc" : "asc";
        $this.attr("data-dir", newDir);
        $("#currentSort").html(dir);
        return false;
    });


    $("#sortRest").on("click", function(){
        tinysort("#sortMe > li", {order:"asc", attr:"data-id"});
        $("#sortByLabel").attr("data-dir", "asc");
        $("#currentSort").html("ID");
        return false;
    });

    $("input:radio").on("click", function(){
        charOrderRules = $(this).attr("value");
        $("#charOrder").html(charOrderRules);
    });
});    

This displays thus:

Current sort: ID
Current charOrder subset: a[àáâãäåæ]
  1. ab
  2. Ac
  3. Àd
  4. [etc]
  5. Cs
  6. Çt
  7. çu


charOrder subset to use:
A
A & C
All

The general conceit is that one can pass a charOrder option to the tinySort call, and this ruleset describes how characters should be interpreted for the sort. For example - if I'm understanding the docs - then a[àáâãäåæ] means that all the accented variations on a will be treated equal to a for the purposes of sorting. The code just lets me sort an array of <li> elements, and select different rulesets to demonstrate the issue.

This works fine if the ruleset is just the a variations. As soon as I add another rule for other letters, the sorting goes pear-shaped.

I have to admit I can't be arsed looking through the tinysort code, although I am going to syart doing that now. Currently I just am after a sanity check of my own code.

One can see this code in action up here: http://adamcameroncoldfusion.cfmldeveloper.com/tinysort/min.html.

Thoughts? And cheers for any help.

--
Adam