Case insensitive sorting in a Dojo Store

On October 24, 2013, in Javascript, by Anuj Gakhar

Dojo has this concept of Store’s which is a uniform way of accessing and manipulating stored data. The Store API is a standard API implemented by all the different kinds of Stores (Memory, JsonRest etc). The Store’s query() function returns filtered data from the store based on passed in criteria and optionally also sorts the result data based on the sort attributes passed in. It’s possible to pass in multiple sort attributes to the query function. An example below :-

    var results = objStore.query(
        {firstName: 'Andrew'},
        {
            sort: [
                {attribute: 'lastName', descending: false},
                {attribute: 'firstName', descending: false}
            ]
        }
    )

This is assuming your store is in a variable called objStore, obviously. This is all fine and cool except that this only does case-sensitive sorting. If you look at the source of the SimpleQueryEngine.js on GitHub, you can see that that clearly it is not using toLowerCase() or toUpperCase() to make a like for like comparison.

    if(sortSet){
        results.sort(typeof sortSet == "function" ? sortSet : function(a, b){
            for(var sort, i=0; sort = sortSet[i]; i++){
                var aValue = a[sort.attribute];
                var bValue = b[sort.attribute];
                if (aValue != bValue){
                    return !!sort.descending == (aValue == null || aValue > bValue) ? -1 : 1;
                }
            }
            return 0;
        });
    }

If you notice, the code above is looking for a sortFunction to be passed in and failing that it provices it’s own sort function to the results.sort() function. So, we can make use of that and provide our own sort function and include case-insensitive sorting within that. And that’s actually quite easy. The above query can be rewritten as below and we have case-insensitive sorting.

    var results = objStore.query(
        {firstName: 'Andrew'},
        { sort: getSortFunction()}
    );
    getSortFunction: function () {
        var _sortArray = [],
            _sortFn,
            sort,
            i;
        _sortArray = [
            {attribute: 'lastName', descending: false},
            {attribute: 'firstName', descending: false}
        ];
        _sortFn = function (a, b) {
            var aValue, bValue;
            for (i = 0; i < _sortArray.length; i++) {
                sort = _sortArray[i];
                aValue = a[sort.attribute].toLowerCase();
                bValue = b[sort.attribute].toLowerCase();
                if (aValue !== bValue) {
                    return !!sort.descending === (aValue === null || aValue > bValue) ? -1 : 1;
                }
            }
            return 0;
        };
        return _sortFn;
    }

All we have done here is provided our own sort function and made both the compared values toLowerCase().

Tagged with:  

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 446 other subscribers

© 2011 Anuj Gakhar
%d bloggers like this: