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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:06:10+00:00 2026-05-15T15:06:10+00:00

Using PHP, I scan a directory and list all of the .xml files. Each

  • 0

Using PHP, I scan a directory and list all of the .xml files. Each XML file contains both a “name” element and a “date” element. The “name” element for each XML file is listed as an option in the select list. This works perfectly fine, however, the “date” element in each XML file is different and contains a date format like this: mm/dd/yyyy. What I am trying to figure out how to do is sort the items according to the date, with the earliest date being first one the list and the most recent at the end.

Now say each of those items has a different value for the “date” element. I need to sort those items with the earliest date being first. I’m not sure how I can store the “date” element data somewhere so that it can be handled by JavaScript. I’m sorry if this is a very vague description, it’s been baffling me for a while and it’s a been confusing to try and explain.

UPDATED

So right now this is what I have working:

<select name="sel_id" onchange="this.form.submit()" size="20">
<option value="item1">Item 1</option>
<option value="item2">Item 2</option>
<option value="item3">Item 3</option>
<option value="item4">Item 4</option>
</select>

I guess one thing that would majorly help is knowing if there’s a way to store the date somewhere in the tags besides the value attribute seeing how it’s already being used. The date itself isn’t a concern, I have that much figured it, it’s just a matter of storing it somewhere so that it can be called client side.

  • 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-15T15:06:11+00:00Added an answer on May 15, 2026 at 3:06 pm

    Updated

    You need to:

    • use a custom attribute for the dates
    • use sort() function’s custom compare function parameter
    • convert to array to make sorting possible
    • convert the dates for string comparison (mm/dd/yyyy -> yyyy-mm-dd)

    See it in action

    [Tested on: IE 5.5+, FF 2+, Chrome 4+, Safari 4+, Opera 9.6+]

    HTML:

    <select name="sel_id" id="sel_id" size="4">
      <option value="item2" date="02-01-2009">Item 2</option>
      <option value="item3" date="01-05-2010">Item 3</option>
      <option value="item1" date="10-06-2007">Item 1</option>
      <option value="item4" date="04-05-2011">Item 4</option>
    </select>
    

    Javascript:

    // array functions are not working
    // on nodeLists on IE, we need to
    // to convert them to array
    function toArray( obj ) {
      var i, arr = [];
      for ( i = obj.length; i--; ){
        arr[i] = obj[i];
      }
      return arr;
    }
    
    // custom compare function for sorting
    // by the hidden date element
    function sortByDate( el1, el2 ) {
      var a = convertToDate( el1.getAttribute("date") ),
          b = convertToDate( el2.getAttribute("date") );
      if ( a < b ) return -1;
      else if( a > b ) return 1;
      else return 0;
    }
    
    // convert date for string comparison
    function convertToDate( date ) {
      date = date.split("-");
      return date[2] + "-" + date[0] + "-" + date[1];
    }
    
    // select the elements to be ordered
    var itemsId = document.getElementById("sel_id"),
        items   = itemsId.getElementsByTagName("option"),
        len     = items.length;
    
    // convert to array, to make sorting possible
    items = toArray( items );
    
    // do the item sorting by their date
    items = items.sort( sortByDate );
    
    // append them back to the parent in order
    for ( var i = 0; i < len; i++ ) {
      itemsId.appendChild( items[i] );
    }
    

    ​
    ​

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

Sidebar

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.