Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8418091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:13:27+00:00 2026-06-10T02:13:27+00:00

In my sortable table, I have null s being sorted to the bottom, numbers

  • 0

In my sortable table, I have nulls being sorted to the bottom, numbers parsed into integers, and strings lowercased, all through a series of if/else statements.

I am trying to get the strings with special characters sorted at the top.

  • If I give it a value of a number, it will go into the number loop and parsed.

  • If I give it a null value, it will go into the null loop.

How would I go about this?

$('#column1').click(function(e) {

    String.prototype.trim = function()
    {
    return this.replace(/^\s+|\s+$/g,"");
    }

    String.prototype.ltrim = function() 
    {
    return this.replace(/^\s+/,"");
    }

    String.prototype.rtrim = function()
    {
    return this.replace(/\s+$/,"");
    }
    var specChar = /[a-zA-Z1-9]/;
    var $sort = this;
    var $table = $('#sort-table');
    var $rows = $('tbody > tr',$table); 

    if($($sort).attr('class')== 'asc'){

        $rows.sort(function(a, b){
        var keyA = (($('td:eq(0)',a).text()).toLowerCase()).trim();
        var keyB = (($('td:eq(0)',b).text()).toLowerCase()).trim();

            if(!isNaN(keyA) || !isNaN(keyB) )
            {               
                if(keyA.length <1 || keyB.length < 1)
                    {
                        if(keyA.length <1  && keyB.length >1)
                            {
                            keyA = "zzzzzzzzzzzzzzz";
                            }
                        else if(keyB.length <1  && keyA.length >1)
                        {
                            keyB = "zzzzzzzzzzzzzzz";
                        }

                        else
                        {
                            keyA="zzzzzzzzzzzzzzz";
                            keyB="zzzzzzzzzzzzzzz";
                        }
                    }
                else
                {
                    if(!isNaN(keyA) && isNaN(keyB))
                    {
                        keyA= parseInt(($('td:eq(0)',a).text()));
                    }

                    else if(!isNaN(keyB)&& isNaN(keyA))
                    {
                        keyB= parseInt(($('td:eq(0)',b).text()));
                    }

                    else
                    {
                        keyA= parseInt(($('td:eq(0)',a).text()));
                        keyB= parseInt(($('td:eq(0)',b).text()));
                    }

                }
            }
            else if ( specChar.test[keyA] == true || specChar.test[keyB] == true)
            {
                console.log("this worked " + keyA + " " + keyB)
                if(specChar.test[keyA] == true && specChar.test[keyB] == false)
                {
                    keyA = null;
                }
                else if(specChar.test[keyB] == false && specChar.test[keyA] == false)
                {
                    keyB = null;
                }
                else
                {
                    keyA = null;
                    keyB = null;
                }
            }
            else
            {
            }
            $($sort).attr('class', 'desc');
            return (keyA < keyB) ? 1 : -1;


        }); 

    $.each($rows, function(index, row){
    $table.append(row);
    $("tr:even").css("background-color", "#FF004D");
    $("tr:odd").css("background-color", "#FFDD00"); 

    }); 

    } 

    else {
            $rows.sort(function(a, b){
                var keyA = (($('td:eq(0)',a).text()).toLowerCase()).trim();
                var keyB = (($('td:eq(0)',b).text()).toLowerCase()).trim();
                if(!isNaN(keyA) || !isNaN(keyB) )
                {                   
                    if(keyA.length <1 || keyB.length < 1)
                        {
                            if(keyA.length <1  && keyB.length >1)
                                {
                                keyA= "zzzzzzzzzzzzzzz"
                                }
                            else if(keyB.length <1  && keyA.length >1)
                            {
                                keyB= "zzzzzzzzzzzzzzz"
                            }

                            else    
                            {
                                keyA= "zzzzzzzzzzzzzzz"
                                keyB= "zzzzzzzzzzzzzzz"
                            }
                        }
                    else
                    {
                        if(!isNaN(keyA) && isNaN(keyB))
                        {
                            keyA= parseInt(($('td:eq(0)',a).text()));
                        }

                        else if(!isNaN(keyB)&& isNaN(keyA))
                        {
                            keyB= parseInt(($('td:eq(0)',b).text()));
                        }

                        else
                        {
                            keyA= parseInt(($('td:eq(0)',a).text()));
                            keyB= parseInt(($('td:eq(0)',b).text()));
                        }

                    }
                }
            else if ( specChar.test(keyA) == false || specChar.test(keyB) == false)
            {
                console.log("this worked " + keyA + " " + keyB)
                if(specChar.test(keyA) == false)
                {
                    keyA = " ";
                }
                else if(specChar.test(keyB) == false)
                {
                    keyB = " ";
                }
                else
                {
                    keyA = " ";
                    keyB = " ";
                }
            }
                else{}

            $($sort).attr('class', 'asc');
            return (keyA > keyB) ? 1 : -1;
            });     
            $.each($rows, function(index, row){
            $table.append(row);
            $("tr:even").css("background-color", "#FF004D");
            $("tr:odd").css("background-color", "#FFDD00"); 
            });
        }

        e.preventDefault();

    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:13:29+00:00Added an answer on June 10, 2026 at 2:13 am

    I’ve create a Fiddle that sorts an array.

    JavaScript

    var array = ["a", "!", "2", "", "b", "1", null, "C"],
        sortedArray;
    
    sortedArray = array.sort(function(a, b) {
        if (a === "") return 1;
        if (b === "") return -1;
        if (a === null) return 1;
    
        if (typeof a === 'string') {
            a = a.toLowerCase();
        }
        if (typeof b == 'string') {
            b = b.toLowerCase();
        }
    
        return b < a ? 1 : -1;
    });
    
    console.log(sortedArray); // => ["!", "1", "2", "a", "b", "C", null, ""]​
    

    You can probably tweak it further to your own taste.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to follow http://www.foliotek.com/devblog/make-table-rows-sortable-using-jquery-ui-sortable/ And have created a very simple example http://jsfiddle.net/6wbZQ/ It's
I have table defined with FlexiGrid. Call to all variables is ok. One column
I have a sortable table in html and looking to put an add.png inside
I have a very simple HTML table, to which jQuery sortable is applied so
I have a table I'm using jQuery UI's sortable on. In the table, I
I have a sortable default table model with ListSelectionListener that listens for a doubleclick
First the example: http://jsfiddle.net/5S3mk/3/ I have a table whose columns need to be sortable
I have a table that's sortable. Within each row is a link that dynamically
I am trying to make my table be sortable. I have found this script
I'm trying to have a table with sortable rows - but also one of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.