Results 1 to 3 of 3

Thread: [Resolved] - Trying to create a custom sort function for an existing script

  1. #1
    Join Date
    Oct 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [Resolved] - Trying to create a custom sort function for an existing script

    Edit - Got the code from the developer


    I need some help modifying a script I've come across from here. It's a great script that allows for dynamic table sorting, my challenge is that it really screws up the appearance of my table which is very reliant on CSS. Basically what's happened is that every th that I've designated as class="sortable" (as per the script) is having the CSS's formatting overwritten. Through looking over some of his sample code I've determined that if I can create a sortable function I can use CSS to customize it.

    Example: Instead of using class="sortable" to sort a numeric column I can create a function that does exactly the same thing but name it class="sortable-sortNumber" and then in CSS I can customize it by using th.sortable-sortNumber.

    The problem is that I have no idea, even after looking through his code, on how to create my own functions (two for numeric, two for text, one for date). All these features are present however I need to create five new functions so I can customize them.

    Here's an example of one of his "custom sort functions" for an IP address.
    Code:
    /*
       sortIPAddress
       -------------
       
       This custom sort function correctly sorts IP addresses i.e. it checks all of the address parts and not just the first.
    
       The function is "safe" i.e. non-IP address data (like the word "Unknown") can be passed in and is sorted properly.
    */
    function sortIPAddress(a,b) {
            var aa = a[fdTableSort.pos];
            var bb = b[fdTableSort.pos];
    
            return aa - bb;
    }
    function sortIPAddressPrepareData(tdNode, innerText) {
            // Get the innerText of the TR nodes
            var aa = innerText;
    
            // Remove spaces
            aa = aa.replace(" ","");
    
            // If not an IP address then return -1
            if(aa.search(/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/) == -1) return -1;
    
            // Split on the "."
            aa = aa.split(".");
    
            // If we don't have 4 parts then return -1
            if(aa.length != 4) return -1;
            
            var retVal = "";
            
            // Make all the parts an equal length and create a master integer
            for(var i = 0; i < 4; i++) {
                    retVal += (String(aa[i]).length < 3) ? "0000".substr(0, 3 - String(aa[i]).length) + String(aa[i]) : aa[i];
            }
            
            return retVal;
    }
    If it helps at all here's the actual script itself in the attachment. I know what I'm trying to do is bound to be simple as hell... I just don't know a lick of Java and I need this done in the next couple days on top of a bunch more site coding I'm working with. Thanks a ton for any help you can spare
    Last edited by toastysquirrel; 10-11-2006 at 04:33 PM. Reason: solved

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Dead link.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Oct 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Whoops, my bad all fixed.

Bookmarks

Posting Permissions

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