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 7861741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:51:30+00:00 2026-06-02T22:51:30+00:00

I have a JS sort script, but is ordering #’s like this: 1. 10.

  • 0

I have a JS sort script, but is ordering #’s like this:

1.
10.
11.
2.
3.
4.

I want them to be ordered by value, like this:

1.
2.
3.
4.
10.
11.

I cannot figure this out. I know there is a way to do it with MySQL, but I am unfamiliar with javascript. Here is what I have so far, but it is ordering numeric numbers incorrect. I appreciate any help you can provide, or just shooting me the right direction.

JavaScript:

var Grid = new Class({
    /** define some variables */
    table: false, 
    headers: false, 
    data: false, 

    /**
     * Initialize the object
     */
    initialize: function(table){
        this.table = table;
        this.getHeaders();
    }, 

    /**
     * Get the headers
     */
    getHeaders: function(){
        this.headers = this.table.getElements('thead tr th');
        this.headers.each(function(h, index){
            //h.store('asc', false);
            if (h.hasClass('sort')) h.addEvent('click', function(){
                if(h.hasClass('asc')){
                    h.className = 'sort desc';

                    //h.addClassName('desc');
                }else{
                    h.className = 'sort asc';
                    //h.addClassName('asc');
                }
                //if (h.retrieve('asc')) h.store('asc', false);
                //else h.store('asc', true);
                //this.sort($('tblrank_id'));
                this.sort(index);
            }.bind(this));
        }, this);
    }, 

    /**
     * Get the table data
     */
    getData: function(){
        this.data = this.table.getElements('tbody tr');
    }, 

    /**
     * Sort the data
     * @param int index
     */
    sort: function(index){
        this.getData();
        data = [];
        sortType = this.headers[index].getProperty('axis');

        asc = this.headers[index].hasClass('asc');//this.headers[index].retrieve('asc');
        if (this.data.length > 0) this.data.each(function(row, i){
            cells = row.getElements('td');
            if (cells.length < this.headers.length) return false;
            value = cells[index].innerHTML;

            if(cells[index].childNodes[0].nodeName.toLowerCase() == "a"){
                //alert(cells[index].childNodes[0].innerHTML);
                value = cells[index].childNodes[0].innerHTML;
            }

            if (sortType == 'int' || sortType == 'float'){
                if (value.contains('$') || value.contains(',')) value = value.replace(/\$/g, '').replace(/,/g, '').toFloat();
                else value = value.toFloat();
            } else if (sortType == 'date') value = Date.parse(value);
            data.push({'index': i, 'value': value, 'row': row});
        }, this);

        if (sortType == 'int' || sortType == 'float' || sortType == 'date') data.sort(this.sortNumeric);
        else data.sort(this.sortCaseInsensitive);
        if (!asc) data.reverse();

        this.data = [];
        data.each(function(d, i){
            this.data.push(d.row);
        }, this);

        this.data.each(function(row, i){
            if (row.hasClass('etblraw0')) row.removeClass('etblraw0');
            if (row.hasClass('etblraw1')) row.removeClass('etblraw1');
            this.table.getElement('tbody').adopt(row.addClass((i % 2 == 0 ? 'etblraw0' : 'etblraw1')));


        }, this);
    }, 

    /**
     * Sort Numerica Values
     * @param object a
     * @param object b
     */
    sortNumeric: function(a, b){


        if ($type(a.value) != 'number') a.value = 0;
        if ($type(b.value) != 'number') b.value = 0;
        return a.value - b.value;
    }, 

    sortCaseInsensitive: function(a, b){
        a.value = a.value.toLowerCase();


        b.value = b.value.toLowerCase();
        if (a.value == b.value) return 0;
        if (a.value < b.value) return -1;
        return 1;
    }
);
  • 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-02T22:51:31+00:00Added an answer on June 2, 2026 at 10:51 pm

    Try this:

    function sortfunc(a,b) {
      return parseInt(a.split(".")[0]) -
             parseInt(b.split(".")[0]);
    }
    
    var list = ["1. first", "2. second", "10. tenth"];
    list.sort(sortfunc);
    
    console.log(list); // => ["1. first", "2. second", "10. tenth"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build advanced search script but I have this error with search_all
This seems very strange to me but I have a script that I'm running
This maybe very simple but I cant seem to sort it out on my
I have done this sort of thing before, but it's not working for me
know nothing about php, but I have this script that reads a folder and
Does anyone happen to have some sort of a nice command line script they
I have a Perl script using a system call to sort a tsv file:
I have some sort of recursive function, but I need to parse a string,
I have this sort of format asp.net MVC View -> Service Layer -> Repository.
I have a sort of unique situation....I want to populate a ModelChoiceField based 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.