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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:07:03+00:00 2026-06-05T19:07:03+00:00

Using Jquery TableSorter, I am creating a custom parser to sort elapsed time <td>

  • 0

Using Jquery TableSorter, I am creating a custom parser to sort elapsed time <td>s that contain “‘#’ year(s) * ‘#’ month(s)”. When I use the function

                $('.techtable td:nth-child(6)').each(function(){
                // console.log($(this));
                var that = $(this).text();
                var myRegexp = /([\d]+) ([\w]+)(?: ([\d]+) ([\w]+))?/;
                var match = myRegexp.exec($(this).text());
                console.log(match);
            });

from the command line, each index contains an array of length 5, looking like this:

["7 months", "7", "months", undefined, undefined]

to this:

["3 years 3 months", "3", "years", "3", "months"]

depending on whether or not the elapsed time has just a month or year element, and then the other. To parse the text, I use regex to gather each element, and then use JS to test whether there are multiple elements or not, and if 1 element only, then wheher it begins with “y” or “m”, and return the number of months, so the parser can sort the <td>s by number of months in integer form.

The parser passes in each element into the function as parameter “s”. when i try regex on “s” directly, it is not returning an array of length 5, it is truncating it to 3 (whether or not I am running the line that truncates it if index 3 is typeof ‘undefined’). When I use the console to directly use this function:

                $('.techtable td:nth-child(6)').each(function(){
                // console.log($(this));
                var that = $(this).text();
                var myRegexp = /([\d]+) ([\w]+)(?: ([\d]+) ([\w]+))?/;
                var match = myRegexp.exec($(this).text());
                if (typeof match[3] == 'undefined') {match.length = 3;};
                console.log(match);
            });

the regex returns the arrays properly, with the array truncated if it only has 1 element (year or month). Here is the code for the custom parser:

                var myRegexp = /([\d]+) ([\w]+)(?: ([\d]+) ([\w]+))?/;
            var match = myRegexp.exec(s);
            var order = [];
            console.log(match);
            if (typeof match[3] == 'undefined') {match.length = 3;};
            // 1 element case:
                // month
                if (match.length = 3) {
                    if (match[2][0] == "m") {
                        order.push(match[1]);
                    }
                // year
                    if (match[2][0] == "y") {
                        order.push(match[1]*12);
                    }
            // both elements
                } else {
                    order.push(match[1]*12 + match[3]);
                }
            s = order;
            return s;
        },

The fiddle is here. The Elapsed parser is second from the bottom of the JS panel. As you can see, since I can’t get the months from the array (indices 4 and 5), I can not calculate the months, and thus the sorting only incorporates years, and the months are sorted by their original HTML placement. What am I missing? (I’m learning…. so direction is appreciated more than an fix, but I won’t turn it down.)

Yes I realize the JS fiddle is loaded (first part is TableSorter, to maintain functionality for verification(click on headers to sort), but all you need to focus on is the last part of the code (reference the ‘//Table Sorter dateSorter’ to see how a correct parser should look). The section ‘//Table Sorter elapsedSorter’ is where my two attempts are, the first part is the working code I use in the console, and the seconde part is the parser, which is somehow deleting the last two indices in the array, thus loosing the month information to calculate.

Guess I’ll have to add Regex, and a personal rating of 1, since I’ve wasted almost an entire day on this.

  • 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-05T19:07:04+00:00Added an answer on June 5, 2026 at 7:07 pm
    if (match.length = 3) {
    

    You meant this?

    if (match.length == 3) {
    

    To help you further, when you write conditions with one constant and a variable, you can write them like this instead:

    if (3 = match.length) {
    

    This would now cause a JavaScript error instead of silently getting turned into an assignment that always yields true.


    In JavaScript, 12 + '4' == '124', so you have to be careful with numbers and the + operator. In languages such as PHP you don’t have this problem, because they have an operator for string concatenations 😉

    var myRegexp = /([\d]+) ([\w]+)(?: ([\d]+) ([\w]+))?/;
    var match = myRegexp.exec(s);
    var order = [];
    
    if (typeof match[3] == 'undefined') {
        if (match[2][0] == "m") {
            order.push(parseInt(match[1]));
        }
        // year
        if (match[2][0] == "y") {
            order.push(parseInt(match[1])*12);
        }
        // both elements
    } else {
        order.push(parseInt(match[1])*12 + parseInt(match[3]));
    }
    s = order;
    return s;
    

    Btw use parseInt(x, 10) if you expect fields to have leading zeroes (which would otherwise result in 0 being returned). Thanks fudgey!

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

Sidebar

Related Questions

I'm using the jQuery tablesorter plugin to sort a table, which assigns .click() handlers
I am using jQuery tablesorter to sort a table and would like to produce
I'm using jQuery Tablesorter to sort a table and everything works as expected. What
I'm using jQuery along with the tablesorter plugin to sort a table. Everything is
I'm using the JQuery tablesorter plugin. The table has a column that shows dates
I'm using the jQuery tablesorter plugin and I have a column that contains name
I am using the jquery tablesorter plugin to sort a table. On of my
I learned that by trying to use the tablesorter plug in from jquery the
I'm using the jQuery Cupertino theme and want to use the icons from that
I am using the jQuery tableSorter plugin on a page. Unfortunatley, the table that

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.