[TWC] Help Sorting a treeview

136 views
Skip to first unread message

Arc Acorn

unread,
May 27, 2015, 6:08:07 PM5/27/15
to tiddl...@googlegroups.com
This is the plugin I'm using:
http://dots.tiddlyspace.com/TreeviewPluginPlugin2


As of now trees with leading numbers sort like:

1 10 100 11 12 13 2 3 4 5...

I know the reason is how strings are sorted and a naturalsort function needs to be worked in:
eg:

function naturalSort(ar, index){
    var L= ar.length, i, who, next,
    isi= typeof index== 'number',
    rx=  /(\.\d+)|(\d+(\.\d+)?)|([^\d.]+)|(\.(\D+|$))/g;
    function nSort(aa, bb){
        var a= aa[0], b= bb[0], a1, b1, i= 0, n, L= a.length;
        while(i<L){
            if(!b[i]) return 1;
            a1= a[i];
            b1= b[i++];
            if(a1!== b1){
                n= a1-b1;
                if(!isNaN(n)) return n;
                return a1>b1? 1: -1;
            }
        }
        return b[i]!= undefined? -1: 0;
    }
    for(i= 0; i<L; i++){
        who= ar[i];
        next= isi? ar[i][index] || '': who;
        ar[i]= [String(next).toLowerCase().match(rx), who];
    }
    ar.sort(nSort);
    for(i= 0; i<L; i++){
        ar[i]= ar[i][1];
    }
}



However I can't for the life of me figure out how to incorporate the natural sort function into the treeview plugin....
Any help would be greatly appreciated.
.

Arc Acorn

unread,
May 30, 2015, 10:22:54 AM5/30/15
to tiddl...@googlegroups.com
The more I tinker with this the more I think that this may require a core modification.
Since it seems like the core issues is how: store.getTaggedTiddlers();
gathers it's list of tiddlers.

Arc Acorn

unread,
Jun 1, 2015, 8:20:21 PM6/1/15
to tiddl...@googlegroups.com
Looking at things store.getTaggedTiddlers() = store.reverseLookup("tags"...etc)
Which sorts it's output by the titles titles using sortTiddlers().

So it looks like the actual problem is somewhere in this part of the core:
Which looks like a basic sort function so I just have to figure out how to turn it into a natural sort function.

// Sort a list of tiddlers
TiddlyWiki.prototype.sortTiddlers = function(tiddlers,field)
{
    var asc = +1;
    switch(field.substr(0,1)) {
    case "-":
        asc = -1;
        field = field.substr(1);
        break;
    case "+":
        field = field.substr(1);
        break;
    }
    if(TiddlyWiki.standardFieldAccess[field]) {
        if(field=="title") {
            tiddlers.sort(function(a,b) {return a[field].toLowerCase() < b[field].toLowerCase() ? -asc : (a[field].toLowerCase() == b[field].toLowerCase() ? 0 : asc);});
        } else {
            tiddlers.sort(function(a,b) {return a[field] < b[field] ? -asc : (a[field] == b[field] ? 0 : asc);});
        }
    } else {
        tiddlers.sort(function(a,b) {return a.fields[field] < b.fields[field] ? -asc : (a.fields[field] == b.fields[field] ? 0 : +asc);});
    }
    return tiddlers;
};

Arc Acorn

unread,
Jun 5, 2015, 2:36:57 PM6/5/15
to tiddl...@googlegroups.com
I finally figured it out!

Though it's probably the most duck tape way I could have done it. XD
Since I have gotten in trouble for sharing question core mods before I won't post the copy/past details.

I did core modification to add my own:
sortTiddlers & reverseLookup functions

I named them sortTiddlersNS & reverseLookupNS

So that way they are only used by the treeviewplugin and shouldn't effect anything else.

in the end I wound up using a slightly modfired version of this natural sorting script:
https://raw.githubusercontent.com/Bill4Time/javascript-natural-sort/master/naturalSort.js

After spending hours figuring out what didn't work I actually learned that as I suspected it was actually super easy to get things to work.



Reply all
Reply to author
Forward
0 new messages