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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:30:19+00:00 2026-05-25T23:30:19+00:00

I have some divs in the page that show different things of the same

  • 0

I have some divs in the page that show different things of the same kind, for example offers, now offers have ending time, and also posted time, if the user wants to order by ending time, or posted time, they should be re ordered.

I’m looking for a javascript solution that could do that, any particular libraries under Ext JS , or JQuery would work

Here is how these divs look like

<div data-sortunit="1" data-sort1="40" data-sort2="156" data-sort3="1"
data-sort4="1317620220" class="item">
</div>

<div data-sortunit="2" data-sort1="30" data-sort2="116" data-sort3="5"
data-sort4="1317620220" class="item">
</div>

<div data-sortunit="3" data-sort1="10" data-sort2="157" data-sort3="2"
data-sort4="1317620220" class="item">
</div>

So I wanna be able to sort these divs based on data-sortN, N being an integer

  • 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-25T23:30:20+00:00Added an answer on May 25, 2026 at 11:30 pm

    Edit: OK, now that you’ve supplied some HTML, here’s javascript code that will sort that specific HTML by the desired column number:

    function sortByDataItem(containerID, dataNum) {
        var values = [];
        $("#" + containerID + " .item").each(function(index) {
            var item = {};
            item.index = index;
            item.obj = this;
            item.value = $(this).data("sort" + dataNum);
            values.push(item);
        });
        values.sort(function(a, b) {return(b.value - a.value);});
        var container = $("#" + containerID);
        for (var i = 0; i < values.length; i++) {
            var self = $(values[i].obj);
            self.detach();
            container.prepend(self);
        }
        return;
    }
    
    
    $("#sort").click(function() {
        var sortValue = $("#sortColumn").val();
        if (sortValue) {
            sortValue = parseInt(sortValue, 10);
            if (sortValue && sortValue > 0 && sortValue <= 3) {
                sortByDataItem("container", sortValue);
                return;
            }
        }
        $("#msg").show(1).delay(5000).fadeOut('slow');
    });
    

    You can see it work here in a jsFiddle: http://jsfiddle.net/jfriend00/JG32X/


    Since you’ve given us no HTML to go on, I’ve made my own HTML and shown you how you can use jQuery to sort:

    HTML:

    <button id="sort">Sort</button><br>
    <div id="productList">
        <div class="row"><div class="productName">Popcorn</div><div class="price">$5.00</div></div>
        <div class="row"><div class="productName">Peanuts</div><div class="price">$4.00</div></div>
        <div class="row"><div class="productName">Cookie</div><div class="price">$3.00</div></div>
        <div class="row"><div class="productName">Beer</div><div class="price">$5.50</div></div>
        <div class="row"><div class="productName">Soda</div><div class="price">$4.50</div></div>
    </div>
    

    Javascript (run after page is loaded):

    $("#sort").click(function() {
        var prices = [];
        // find all prices
        $("#productList .price").each(function(index) {
            var str = $(this).text();
            var item = {};
            var matches = str.match(/\d+\.\d+/);
            if (matches && matches.length > 0) {
                // parse price and add it to the prices array
                item.price = parseFloat(matches[0]);
                item.row = $(this).closest(".row").get(0);
                item.index = index;
                prices.push(item);
            }
        });
        // now the prices array has all the prices in it
        // sort it using a custom sort function
        prices.sort(function(a, b) {
            return(a.price - b.price);
        });
        // now pull each row out and put it at the beginning
        // starting from the end of the prices list
        var productList = $("#productList");
        for (var i = prices.length - 1; i >= 0; i--) {
            var self = $(prices[i].row);
            self.detach();
            productList.prepend(self);        
        }
    });
    

    And, a jsFiddle that shows it in action: http://jsfiddle.net/jfriend00/vRdrA/.

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

Sidebar

Related Questions

I have a page that has some divs. I show images on these divs.
I have some DIVs that I am using JQuery to hide and show using
I have some divs on a page that are hidden via jquery when the
I have some divs that when the page loads are populated with images of
I have some divs on my page that when a link is clicked they
I have a grid of divs that show some of my work (portfolio), and
I have a page with several .desc divs. I want to add some html
I have some divs that have dynamic heights controlled by a 'click' function as
I have some divs that have dynamic heights controlled by a 'click' function as
I have some divs that I want to display under some specific conditions in

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.