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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:41:21+00:00 2026-06-01T11:41:21+00:00

<div class=myClass>1</div> <div class=myClass>2</div> <div class=myClass>3</div> <div class=myClass>4</div> <div class=myClass>5</div> <div class=myClass>6</div> var $myClass

  • 0
<div class="myClass">1</div>
<div class="myClass">2</div>
<div class="myClass">3</div>
<div class="myClass">4</div>
<div class="myClass">5</div>
<div class="myClass">6</div>

var $myClass = $('.myClass');
$myClass.eq(0).text() //returns '1'
$myClass.eq(4).text() //returns '5'
$myClass.eq(5).text() //returns '6'

What I want to do is reorder the objects in jQuery manually.

//fantasy command reversing
$myClass.eqSorter([5,4,3,2,1,0]);
$myClass.eq(0).text() //returns '6'
$myClass.eq(5).text() //returns '1'

What I really want is to allow a fully custom order input

//fantasy command custom ordering
$myClass.eqSorter([3,5,4,2,0,1]);
$myClass.eq(0).text() //returns '4'
$myClass.eq(5).text() //returns '2'

Now I’ve looked at basic ways of doing this like .sort as a part of javascript, but .sort takes a dynamic argument the compares things, and does not allow for a fully custom order. I also looked at making a new blank jquery object that I could pass elements into like this $newObjectVar.eq(0) = $myClass.eq(3); for example. But as of yet I have not found a way to make a blank jQuery object like 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-01T11:41:22+00:00Added an answer on June 1, 2026 at 11:41 am

    You could make a custom method like this that would reorder the existing DOM elements in the jQuery object. One issue is that many operations on jQuery objects sort them into DOM order so they may not stay in this order if you carry out other operations on the jQuery object.

    jQuery.fn.eqSorter = function(array) {
        // get copy of DOM element array
        var copy = this.toArray();
        // don't exceed bounds of arrays
        var len = Math.min(array.length, copy.length);
        for (var i = 0; i < len; i++) {
            var index = array[i];
            // make sure incoming index is within bounds of DOM object array
            if (index < copy.length) {         
                // put the index item into the i location
                this[i] = copy[index];
            }
        }
        return this;
    }
    
    $myClass.eqSorter([5,4,3,2,1,0]);
    

    After some more thinking, here’s another way of doing things that is also a lot more robust as it prevents dups and holes even if the array passed in has errors in it. The result will be only the elements that match the indexes in the passed in array. Any dups or indexes out of range will be ignored.

    The jQuery way of doing things is generally not to change the DOM elements in a given jQuery object, but to modify the existing jQuery object into a new jQuery object. To implement it that way, with the added robustness you could do this:

    jQuery.fn.eqSorter = function(array) {
        var elems = [], doneIndexes = {}, index;
        for (var i = 0, len = array.length; i < len; i++) {
            index = array[i];
            // if the index is not out of range and it's not a dup, 
            // then get the corresponding DOM element from the jQuery object
            // and put it into the new array
            if (!doneIndexes[index] && index < this.length && index >= 0) {
                elems.push(this[index]);
                doneIndexes[index] = true;
            }
        }
        return this.pushStack(elems, "eqSorter", arguments);
    }
    
    var $myOrderedObj = $myClass.eqSorter([5,4,3,2,1,0]);
    

    This has the following behavior:

    • Any duplicate indexes in the passed in array are ignored (only the first one is processed)
    • Any indexes that are out of range are ignored
    • Any DOM elements in the initial jQuery object that are not referenced by an index will not appear in the result
    • This method returns a new jQuery object and can be used with .end()

    There’s a working testbed here: http://jsfiddle.net/jfriend00/DPhc9/

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

Sidebar

Related Questions

I have a class of div MyClass I want to hook up to two
I have a div having a css class MyClass. Inside this div, I can
In yesod, we can specify the class attribute like so: <div .myclass> ==> <div
My code is like this foreach($array as $key => $value) { <div class=myclass><a href=$key>$value</a></div>
I want to scale the picture to fit the main div class http://jsfiddle.net/EyW99/ Sorry,
<div class = a> <div class = a b> text </div> </div> <script> alert($(.a).html());
I have some HTML that looks like this: <div class=MyClass></div> <div class=MyClass></div> <div class=MyClass></div>
I have the following: <p class=myClass>This is a paragraph <div>div within the paragraph</div> More
I have the following piece of HTML code: <div class=myclass id =class1> <ol class=list>
I have a div with class .myClass, I am cloning it by clicking on

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.