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

As you understand this improvement allows to sort not only by jQuery data() attribute, but for any other attribute or text of the element. My purpose was to make sorting more flexible, but with this feature you are able to avoid attr, data and selector options in fact and extract the necessary data inside callback function.

psolom avatar Mar 29 '16 07:03 psolom

At first glance (without writing/running any code) this looked similar to the sortFunction option. But where sortFunction bypasses all other options completely yours retains them (more or less). This looks like a good addition. The existing sortFunction should then be removed because it adds nothing that cannot be done with your implementation (right?).

As for the name of the option. I'm thinking of something along the lines of subject. Because basically you just return the string that will be sorted to.

On second thought, the existing sortFunction does pass the original positions of the element. So there is some difference there. We could implement those here as well just to be complete.

Sjeiti avatar Apr 02 '16 09:04 Sjeiti

@servocoder got a branch I can check out?

Sjeiti avatar May 06 '16 08:05 Sjeiti

I have lost this from my sight. You are right on the difference sortFunction from my implementation. I guess we should leave sortFunction because it could be used for other cases. Initially I was searching advanced way to get text (subject) for sorting, because CSS selectors don't cover all cases, as you could see the example in my initial message. That's why I had created that callback option. It's some sort of advanced selector and I would name it as selector, but this name is already used for CSS selectors. What about selectorCallback of selectorFunction? It better reflects the purpose of the option than subject for my opinion.

psolom avatar May 06 '16 11:05 psolom