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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:30:10+00:00 2026-06-14T00:30:10+00:00

I’ve been trying to sort my dates by ascending/descending order in jQuery but with

  • 0

I’ve been trying to sort my dates by ascending/descending order in jQuery but with no luck. Any idea to what is going wrong? Here is my code: http://jsfiddle.net/UdwNh/

 var itemsArray = $('div#activity_box');
    itemsArray.sort(function(a,b){
        var aTime = new Date($(a).find('#activity_date').text()).getTime();
        var bTime = new Date($(b).find('#activity_date').text()).getTime();
        return aTime - bTime;
    });

    $('#sortAsc').click(function(){
        $("#wrapper").empty();
        $(itemsArray).each(function(){
            $("#wrapper").prepend($(this));
        });
    });

    $('#sortDesc').click(function(){
        $("#wrapper").empty();
        $(itemsArray).each(function(){
            $("#wrapper").append($(this));
        });
    });​

Relevant HTML:

<input class="btn" type="button" value="Sort Ascending" id="sortAsc"/>
<input class="btn" type="button" value="Sort Descending" id="sortDesc"/>

<div id="wrapper">
    <ul>
    <li class='item'><div id='activity_date'>01/10/2012</div>
    <div id='activity_box'>
    <div id='activity_text' class='windowClass2'>...text here...</div>
    </div></li><li class='item'><div id='activity_date'>04/10/2012</div>
    <div id='activity_box'>
    <div id='activity_text' class='windowClass2'>...text here...</div>
    </div></li><li class='item'><div id='activity_date'>10/10/2012</div>
    <div id='activity_box'>
    <div id='activity_text' class='windowClass2'>...text here...</div>
    </div></li><li class='item'><div id='activity_date'>16/10/2012</div>
    <div id='activity_box'>
    <div id='activity_text' class='windowClass2'>...text here...</div>
    </div></li>
    </ul>
</div>
  • 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-14T00:30:12+00:00Added an answer on June 14, 2026 at 12:30 am

    There are several issues with that code.

    1. You’re using the same id value on more than one element. id values must be unique on the page.

      To fix it, use a class value instead, or a data-* attribute, or anything else that’s allowed to occur more than once on a page.

    2. You’re calling sort on a jQuery instance. jQuery has no documented sort function. It may have an undocumented one, but if so, you have no idea what it does. It’s certainly doesn’t make sense to assume it will rearrange DOM elements.

      To fix it, you’ll have to write your own function for that (it’s not complicated), or find a plug-in that does it for you.

    3. Even if sort did work, the date strings you’re passing into new Date(string) are in the form 20/10/2012, 17/10/2012, and such. That isn’t a format that new Date(string) is documented to understand. The only string format that new Date(string) is documented to understand is a simplified form of ISO-8601, which doesn’t look like that. And in fact, new Date("20/10/2012") fails on Chrome and probably other browsers.

      To fix it, you’ll need to get the dates another way. You could store the milliseconds-since-the-Epoch value on the elements as a data-* attribute and then use it directly. Or you could parse the date strings you have yourself. If they’re always in that dd/mm/yyyy format, parsing the individual parts into numbers is trivial, and then you can pass the resulting numbers for the year, month, and day into new Date(year, month, date). (Remember that months start with 0 = January.)

    4. You’re putting in a closing </li> in at the outset, where there’s no opening <li>.

      To fix it, don’t do that. 🙂

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.