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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:39:46+00:00 2026-06-14T09:39:46+00:00

I have a pretty weird problem with sorting in JavaScript. I’ve wandered into this

  • 0

I have a pretty weird problem with sorting in JavaScript. I’ve wandered into this wonderfully and horribly confusing world of sorting, and was wondering if you guys could help me out:

Basically, I’m trying to mimic the tabindex HTML attribute, which basically changes the order of ‘tabbing’ through the page: Elements with a tabindex are first, with the smallest positive number first, going down, and the elements without tabindex are last in an unchanged order. So, it’s like this:

tabindex=1, tabindex=2, tabindex=5, tabindex=5000, no tabindex, no tabindex, no tabindex, etc…

I’m trying to get this working. In Chrome it doesn’t work because of (I think, at least) the sorting algorithm it uses compared to all other major browsers.

What’s returned in Chrome is sporadic and weird, with one element completely out of order and other elements that used to be at the top instead at the bottom.

In all other browsers, it works (except the tabindex’d elements are in a reversed order, but that’s really easy to fix, that I was planning on doing after I got this working). This is using the following code:

selectElements.sort(function(a,b){
    return b.getAttribute('tabindex')-a.getAttribute('tabindex');
});

I believe the problem may stem from the fact that elements without getAttribute are returned null, but what do I know… 😛

Let me know what you guys would do to make this work… Thanks!

  • 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-14T09:39:47+00:00Added an answer on June 14, 2026 at 9:39 am

    Improving on the answer from @3dgoo, the sort method should check for equality first:

    selectElements.sort(function(a, b) {
        a = a.getAttribute('tabindex');
        b = b.getAttribute('tabindex');
    
        if (a === b) {
            return 0;
        } else if (a === null) {
            return -1;
        } else if (b === null) {
            return 1;
        }
    
        return a < b ? -1 : 1;
    }); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty weird problem related to zend framework's zend_db. I use $this
I'm doing some pretty unholy things with JavaScript, and I've run into a weird
I have a pretty weird problem in my iPhone app which is, I think,
I have run into this problem a lot in the past, but never really
I have this weird problem where my Google Maps API script doesn't work, no
I've a weird problem with malloc. I have this typedefs: typedef struct buffer {
This is the second time that I have this really weird problem with WCF
I have pretty simple jquery code : $(document).ready(function(){ $('img.marqFl').on({ mouseenter: function() { $(this).animate({height: 300},
I have pretty good skills in PHP , Mysql and Javascript for a junior
I have a rather small but weird problem that I can't seem to fix.

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.