Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 Posts
    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.
    Last edited by felgall; 01-26-2012 at 07:26 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    sephlaire (01-26-2012)

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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;
      },


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •