TinySort icon indicating copy to clipboard operation
TinySort copied to clipboard

New option to sort by callback function

Open psolom opened this issue 8 years ago • 4 comments

Hi, I have implemented new improvement that allows to sort elements by callback function. I am ready to create new Pull Request, just want to describe it before and ask your advice:

On last project I faced with a problem when I had to sort elements by jQuery data() object. Each element contained a lot of build in a complex structure and it would be to messy to print all data as data attributes. To solve the problem I have implemented new callback option. Now it is possible to pass anonymous function that accepts element as first argument, so it is possible to manipulate with element and extract required data from element to sort by. Example:

items.tsort({order: 'asc', natural: true, callback: function(el) {
    var sortField = configSortField, // some global var
        itemData = $(el).data('itemdata');

    switch(sortField) {
        case 'type':
            return itemData['prop']['type'];
        case 'size':
            return itemData['prop']['size'];
        case 'modified':
            return itemData['prop']['date'];
        default:
            return itemData['name'];
    }
}});

I have tested it and it works well. I just want to ask your advice about the name of the option. Not sure callback is appropriate one. What do you think?

psolom avatar Mar 29 '16 07:03 psolom