Jump to content


Photo

Sorting Help Please


  • Please log in to reply
2 replies to this topic

#1 Beauchy

Beauchy
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 11 June 2006 - 08:35 PM

Cannot get it to sort according to date...

The problem is that I cannot change the file names but they are all in this format:

JAREPORT 05-05-06.txt
JAREPORT 05-07-06.txt
JAREPORT 06-01-06.txt

I want to display the files as links in a table according to month in descending order... eg 31, 30, 29, etc...

Everything works except the sorting...

How can I sort those files?

Thanks, Beauchy

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 12 June 2006 - 02:57 AM

Put them in an array and use a custom sort function. For example, this sorts the middle part of the date in descending order
$files = array(
    'JAREPORT 05-05-06.txt',
    'JAREPORT 05-07-06.txt',
    'JAREPORT 06-01-06.txt'
);

usort ($files, 'monthsort');

// check results
echo '<pre>', print_r($files, true), '</pre>';

function monthsort($a, $b) {
   $ma = substr($a, 12, 2);
   $mb = substr($b, 12, 2);
   return -strcmp($ma, $mb);
}    

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Beauchy

Beauchy
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 June 2006 - 04:51 AM

Got it working!! [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Thank you very much! [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users