Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-26-2012, 04:10 PM   PM User | #1
sephlaire
New Coder

 
Join Date: Jan 2012
Posts: 12
Thanks: 6
Thanked 0 Times in 0 Posts
sephlaire is an unknown quantity at this point
Sorting After regular Expression match

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]]
[[wiki:whoops:etchers|Elephants]]

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

Code:
         else if (text.match(/\[\[(.*):(.*)\]\]/)) {  
         return sorttable.sort_link;
        }
The second part is the actual sorting:
Code:
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.
sephlaire is offline   Reply With Quote
Old 01-26-2012, 07:23 PM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 01-26-2012 at 07:26 PM..
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
sephlaire (01-26-2012)
Old 01-26-2012, 08:02 PM   PM User | #3
sephlaire
New Coder

 
Join Date: Jan 2012
Posts: 12
Thanks: 6
Thanked 0 Times in 0 Posts
sephlaire is an unknown quantity at this point
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

Last edited by sephlaire; 01-26-2012 at 08:06 PM..
sephlaire is offline   Reply With Quote
Old 01-26-2012, 08:23 PM   PM User | #4
sephlaire
New Coder

 
Join Date: Jan 2012
Posts: 12
Thanks: 6
Thanked 0 Times in 0 Posts
sephlaire is an unknown quantity at this point
So this is where I'm at now. For some reason unknowing to me I cannot convert to lowercase.
Code:
 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;
  },
sephlaire is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:48 PM.


Advertisement
Log in to turn off these ads.