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

  • Home
  • SEARCH
  • 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 6227579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:14:59+00:00 2026-05-24T09:14:59+00:00

I am looking for some help in sorting div’s with PrototypeJS. Here is what

  • 0

I am looking for some help in sorting div’s with PrototypeJS. Here is what I have so far:

document.observe('dom:loaded',function(){
    $$('.sortcol').invoke('observe', 'click', function() { 
        if (this.hasClassName('desc')) {
            var desc = false;
            this.removeClassName('desc');
        } else {
            var desc = true;
            this.addClassName('desc');
        }
        var colname = this.className;
        var contentid = this.up(2).id;
        sortColumn(contentid,colname,desc);
    });
});

function sortColumn(contentid,colname,desc) {
    $$('#'+contentid).select('.'+colname).sort(function(a,b){ 
        if (desc) {
            return (a.text.toLowerCase() >= b.text.toLowerCase() ) ? -1 : 1; 
        } else {
            return (a.text.toLowerCase() < b.text.toLowerCase() ) ? -1 : 1; 
        }
    });
} 

Example data:

<div id="contentbox_Users" class="userList">
    <div class="userListHeader">
        <div class="userListHeaderCell col1">First Name</div>
        <div class="userListHeaderCell col2">Last Name</div>
    </div>
    <div id="contentbox_People">
        <div class="userListRow">
            <div class="userListCell col1">John</div>
            <div class="userListCell col2">Smith</div>
        </div>
        <div class="userListRow">
            <div class="userListCell col1">Bob</div>
            <div class="userListCell col2">Ray</div>
        </div>
        <div class="userListRow">
            <div class="userListCell col1">Fred</div>
            <div class="userListCell col2">Jones</div>
        </div>
    </div>
</div>

Basically anything with a class “sortcol”, when it is clicked, I want it to sort by the column name clicked (class). The first issue is I need to be able to get the class name correctly when there is multiple classes. The classes are all like col1, col2, etc. How would I find the correct class?

The second thing is changing sortColumn so that it keeps column data together (each row is wrapped by another div) and output the result, replacing the current data.

This needs to be done in prototypejs and I can’t change the code to tables.

Thanks in advance for the help.

  • 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-05-24T09:14:59+00:00Added an answer on May 24, 2026 at 9:14 am

    For the first part of your question it would be much easier if the column name was it’s own attribute like rel or data-*, but you say you cannot change the HTML. It is possible to pick out the likeliest class with regex…

    var colname = this.className.match(/\bcol\d+\b/).first()
    

    But this is unnecessary if we assume every row has the same columns in the same order. This would be a safer assumption if a table were used.

    var colnumber = this.up().childElements().indexOf(this);
    

    The second part of your question is easy, just sort the rows instead of the cells.

    Your draft sortColumn function doesn’t actually change the elements – select returns an array of element references, not their container – so you need to do something with the resulting array. Luckily any append or insert action of an element causes it to be removed from it’s parent first, so simply append them once more and they’ll assume the correct order. No replacing is needed, I’ve seen libraries that bizarrely convert the elements to HTML, concatenate that then reinsert it!?!

    The following has been tested.

    document.observe('dom:loaded',function() {
        $$('.userListHeaderCell').invoke('observe', 'click', function() {
            this.toggleClassName('desc');
            var colnumber = this.up().childElements().indexOf(this);
            var content = this.up(2); // use the element directly instead of it's ID
            sortColumn(content, colnumber, this.hasClassName('desc'));
        });
    });
    
    function sortColumn(content, colnumber, desc) {
        content.select('.userListRow').sort(function(a,b){
            var atext = a.down(colnumber).innerHTML.stripTags().toLowerCase();
            var btext = b.down(colnumber).innerHTML.stripTags().toLowerCase();
            return atext.localeCompare(btext) * (desc ? -1 : 1);
        }).each(Element.prototype.appendChild, content);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for some direction here as I'm running into some migration problems. We have
I'm looking for some help from a regex guru, here's what I want to
I'm looking for some help. I've tried searching this site and have tried amending
Im looking for some help on information on files using Power shell, we have
I am once again looking for some help. I have found this stopwords script
Looking for some help with a Labview data collection program. If I could collect
Hi I'm looking for some help in mapping the following tables to a hibernate
I'm looking for some help on my discussion. We're discussing two solutions to a
I'm looking for some help with a dependency issue. In a nutshell, I've included
HI, I am new to the scrum methodology and looking for some help to

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.