Jump to content

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


toasty

Recommended Posts

[b]Edit - Got the code from the developer[/b]

I need some help modifying a script I've come across from [url=http://www.frequency-decoder.com/2006/09/16/unobtrusive-table-sort-script-revisited]here[/url].  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 [b]th[/b] that I've designated as [color=green]class="sortable"[/color] (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 [color=green]class="sortable"[/color] to sort a numeric column I can create a function that does exactly the same thing but name it [color=green]class="sortable-sortNumber"[/color] and then in CSS I can customize it by using [color=green]th.sortable-sortNumber[/color].

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 [b]create five new functions so I can customize them[/b].

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;
}[/code]

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 :D

[attachment deleted by admin]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.