Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds alphanumeric sorting reader option #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions jquery.dynatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,42 @@
// force false boolean value to -1, true to 1, and tie to 0
return comparison === false ? -1 : (comparison - 0);
},
alphaNumeric: function(a, b, attr, direction) {
//swap if reverse
if(direction === -1) {
var temp = a;
a = b;
b = temp;
}

//extract display text
a = $(a[attr.toString()]).text();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop the toString() on the attr names, since names are always strings anyway regardless of using integers, etc to refer to them.

b = $(b[attr.toString()]).text();

var aMatch, bMatch, a1, b1, i= 0, n,

regex =/(\.\d+)|(\d+(\.\d+)?)|([^\d.]+)|(\.\D+)|(\.$)/g;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment here that links to the URL this regex came from if copied.


//equal
if(a === b) return 0;

//match
aMatch = a.toLowerCase().match(regex);
bMatch = b.toLowerCase().match(regex);

//sort
while(i< aMatch.length){
if(!bMatch[i]) return 1;
a1= aMatch[i],
b1= bMatch[i++];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a little less confusing; we're doing two different things with this one line. We're first grabbing bMatch[i] and then incrementing i for the next loop iteration., so if i is 0, then we're setting b1 = bMatch[0] and then incrementing i to 1 in the same line which can throw people off who think that the return value of i++ is the new value of i rather than the old.

if(a1!== b1){
n= a1-b1;
if(!isNaN(n)) return n;
return a1>b1? 1:-1;
}
}
return b[i]? -1:0;
},
originalPlacement: function(a, b) {
return a['dynatable-original-index'] - b['dynatable-original-index'];
}
Expand Down