View Full Version : Sorting After regular Expression match

01-26-2012, 05:10 PM
I still working on this script for sorting fields in a wiki and have ran into another feature I'm trying to get working.

Right now I have a long list that needs to be sorted and it looks like this:

[[wiki:software:softwarename|Display Text]]
[[wiki:somethingelse:alphaname|Alpha Text]]

The first part of my code is an attempt to match those with a regular expression:

else if (text.match(/\[\[(.*):(.*)\]\]/)) {
return sorttable.sort_link;

The second part is the actual sorting:

sort_link: function(a,b) {
aa = a[0].split(":",4);
bb = b[0].split(":",4);

if (aa[3]==bb[3]) {return 0;}
if (aa[3]<bb[3]) {return -1;}
return 1;

I'm pretty sure the regular expression works... at least it works on some regular expression "testings" websites. But I just cant get these links to sort. I am trying to wort by the Display text which is located after the '|' in the text.

01-26-2012, 08:23 PM
You are splitting the string on : not on |

As there are two : in each string that splits it into three pieces

You then compare the fourth pieces

For the code to do what you say you want it to do you either need to change the | to a : in each string or change it to split on | and then compare the second pieces aa[1]==bb[1] etc.

The array JavaScript returns for regular expressions does not reserve the first position the way some server side languages do.

01-26-2012, 09:02 PM
Thanks!, sometimes I guess I overlook the simple things like 1 wrong character. On another note I almost thought it wasnt sorting right but then i noticed its case sensitive sorting and lowercase doesnt get sorted until way after all uppercase. A .toLowerCase() somewhere would probably help.
again thanks for the help

01-26-2012, 09:23 PM
So this is where I'm at now. For some reason unknowing to me I cannot convert to lowercase.

sort_link: function(a,b) {
a[0] = a[0].toLowerCase();
b[0] = b[0].toLowerCase();

aa = a[0].split("|",2);
bb = b[0].split("|",2);

if (aa[1]==bb[1]) {return 0;}
if (aa[1]<bb[1]) {return -1;}
return 1;