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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:34:32+00:00 2026-06-01T15:34:32+00:00

Inspired by chapter 7 of Learning jQuery 1.3 (not found in the third edition),

  • 0

Inspired by chapter 7 of Learning jQuery 1.3 (not found in the third edition), I’ve put together this sort routine:

var rows = $table.find('tbody > tr').get();
$.each(rows, function(index, row) {
      var $cell = $(row).children('td').eq(column);
      $(row).data('sortKey',$cell);
});
rows.sort(function(a, b) {
      if ($(a).data('sortKey') < $(b).data('sortKey'))
            return -sortDirection;
      if ($(a).data('sortKey') > $(b).data('sortKey')) 
            return  sortDirection;
      return 0;
});

But I don’t like using $(a) and $(b) on every row.

Q: Is there a way I can cache $(a) and $(b)? The author uses something he calls an expando instead.

This kind of property, attached to a DOM element but not a normal DOM
attribute, is called an expando. This is a convenient place to store
the key, since we need one per table row element. Now, we can examine
this attribute within the comparator function, and our sort is
markedly faster.

  • 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-01T15:34:33+00:00Added an answer on June 1, 2026 at 3:34 pm

    There are two optimisations you can do here.

    The first is to use $.data rather than $.fn.data:

    $.data(a, 'sortKey') // quick
    $(a).data('sortKey') // slow
    

    This is superior because it does not need to construct a new jQuery object — you are right to think of this as a performance issue.

    The second is to cache the result of data, so you only need to do it once.

    rows.sort(function(a, b) {
        var aKey = $.data(a, 'sortKey'),
            bKey = $.data(b, 'sortKey');
        if (aKey < bKey) return -sortDirection;
        if (aKey > bKey) return  sortDirection;
        return 0;
    });
    

    The third optimisation (since a lonesomeday answer is buy-two-get-one-free) is to use jQuery 1.7, which is much superior to 1.3, both in speed and in other functionality.

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

Sidebar

Related Questions

Inspired by this question. Why is there no list.clear() method in python? I've found
Inspired by this question ( Find an integer not among four billion given ones
Inspired by this CodingHorror article, Protecting Your Cookies: HttpOnly How do you set this
Inspired by this question , I wanted to try my hand at the latest
Inspired by this question and answer , how do I create a generic permutations
Inspired by this question I began wondering why the following examples are all illegal
Inspired by this article , I was playing around with the Ruby method calling
Inspired by this post , I'm trying to use a TreeView inside the AutoCompleteBox's
Inspired by this topic , I decided to write a simple program that does
Inspired by this question on Meta , I wrote two queries on the Stack

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.