Re: [PHP] Re: Sorting times (SOLVED before tedds crappy SOLVED)

2009-02-17 Thread Jochem Maas
Shawn McKenzie schreef:
 tedd wrote:
 At 9:56 AM +0100 2/16/09, Jochem Maas wrote:
 for any reasonable number of items my tests show tedd's version
 pisses on McKenzies from a great height (note that I actually
 optimized Mckenzies variant by halfing the number of calls to
 strtotime()).
 ROTFLOL.   -- I seldom say that!
 
 Haha!  Yes, I was trolling and got a good one from Jochem!  I would say
 ROTFLMAO!
 From a great height!!! Now that's funny!
 
 Too funny.  I'll have to remember to work this in to some conversation
 today at work.  ;-)

fair play, well taken :-) glad we got it sorted out ;-)

 
 I added a third variant, as a sort of control, which runs pretty
 much on par with tedd's version but uses rather less LOC
 (tedd you might like it as a little example of using array_multisort(),
 i.e. a way of avoiding writing the double foreach loop in this case)
 The speed of the sort doesn't matter at all. The maximum number of data
 that needed to be sorted in my problem would have been 126 (18 different
 times for 7 days).

 I only presented part of the problem here. It was a distilled version of
 the problem I was working on, which was to allow people to enter times
 that they were available for tutoring in two hour chunks.

 So, a person might say they were available from 7:00 am to 9:00 am AND
 also state that they were available from 7:30 am to 9:30 am, which was a
 no-no.

 Having two loops allowed me to check after converting to seconds AND
 sorting to see if there were any overlaps. In such case I simply deleted
 the offending data from the array before converting everything back into
 normal time (min:sec).

 Re this post -- all I needed was a push in the right direction. I was
 thinking about sorting, but I didn't even consider converting everything
 to seconds and then sorting.

 That's really all I needed AND another reason why this list is so great!
 Not only did I get a push in the right direction, but I got a good laugh
 out of it -- from great heights indeed. :-)

 Thanks and Cheers,

 tedd

 
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Sorting times (SOLVED before tedds crappy SOLVED)

2009-02-16 Thread Jochem Maas
Shawn McKenzie schreef:
 Shawn McKenzie wrote:

...

 Not tested:

no shit.

 function time_sort($a, $b)
 {
 if (strtotime($a) == strtotime($b)) {
 return 0;
 }
 return (strtotime($a)  strtotime($b) ? -1 : 1;
 }

 usort($time, time_sort);

 Well, I just thought, since the strtotime() uses the current timestamp
 to calculate the new timestamp, if you only give it a time then the
 returned timestamp is today's date with the new time you passed.  If you
 had a large array and the callback started at 23:59:59 then you could
 end up with some times from the date it started and some from the next
 day, which of course would not be sorted correctly with respect to times
 only.  So, this might be better (not tested):


ditto (as in nice syntax error).


 function time_sort($a, $b)
 {
 static $now = time();

 if (strtotime($a, $now) == strtotime($b, $now)) {
 return 0;
 }
 return (strtotime($a, $now)  strtotime($b, $now) ? -1 : 1;
 }


 Your best bet above.

and G.W.Bush is a socialist.

 

I did a little test, the ultra-fast version offered by McKenzie
is really great as long as your array of time strings aren't ever
going to be longer than say 3-4 items (with the caveat that you
don't use a second argument to strtotime() ... if you do then
even with 3 items it's substantially slower).

for any reasonable number of items my tests show tedd's version
pisses on McKenzies from a great height (note that I actually
optimized Mckenzies variant by halfing the number of calls to
strtotime()).

I added a third variant, as a sort of control, which runs pretty
much on par with tedd's version but uses rather less LOC
(tedd you might like it as a little example of using array_multisort(),
i.e. a way of avoiding writing the double foreach loop in this case)

tedd's variant: sortTime1()
McKenzie's variant: sortTime2()
my variant: sortTime3()

sample output from one of my test runs:
===
===
No. of array items = 3, no of iterations = 1
---
timeSort1() ran for 1.306011 seconds
timeSort2() ran for 1.337358 seconds
timeSort3() ran for 1.742724 seconds

No. of array items = 6, no of iterations = 1
---
timeSort1() ran for 2.647697 seconds
timeSort2() ran for 2.475791 seconds
timeSort3() ran for 7.268916 seconds

No. of array items = 9, no of iterations = 1
---
timeSort1() ran for 3.891894 seconds
timeSort2() ran for 3.960463 seconds
timeSort3() ran for 18.440713 seconds




the test script:
===
===
?php
// TEST
ini_set(date.timezone, Europe/Amsterdam);

$iter = 1;
$time = array(
array(1:30pm, 7:30am, 12:30pm),
array(1:30pm, 7:30am, 12:30pm, 4:45pm, 8:15am, 11:00pm),
array(1:30pm, 7:30am, 12:30pm, 4:45pm, 8:15am, 11:00pm, 
4:30am, 6:45am, 12:00pm),
);

foreach ($time as $t)
testIt($t, $iter);


// FUNCS

function sortTime1($in_times)
{
$time = array();
foreach ($in_times as $t)
$time[] = strtotime($t);

sort($time);

$sort_time = array();
foreach ($time as $t)
$sort_time[] = date(g:ia, $t);

return $sort_time;
}

function timeSort2($in)
{
static $time = null;

if (!$time)
$time = time();

$now = array_fill(0, count($in), $time);
$out = array_map(strtotime, $in, $now);
array_multisort($out, SORT_NUMERIC, SORT_ASC, $in);

return $in;
}

function timeSort3($a, $b)
{
static $now = null;

if (!$now)
$now = time();

$a = strtotime($a, $now);
$b = strtotime($b, $now);
if ($a == $b)
return 0;

return $a  $b ? -1 : 1;
}

function testIt($time, $iter)
{
echo \nNo. of array items = , count($time), , no of iterations = 
$iter\n---\n;

$s = microtime(true);
for ($i = 0; $i  $iter; $i++)
timeSort2($time);
$e = microtime(true);
echo timeSort1() ran for .round($e-$s, 6). seconds \n;

$s = microtime(true);
for ($i = 0; $i  $iter; $i++)
timeSort2($time);
$e = microtime(true);
echo timeSort2() ran for .round($e-$s, 6). seconds \n;

$s = microtime(true);
for ($i = 0; $i  $iter; $i++)
usort($time, timeSort3);
$e = microtime(true);
echo timeSort3() ran for .round($e-$s, 6). seconds \n;
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Sorting times (SOLVED before tedds crappy SOLVED)

2009-02-16 Thread German Geek
Remember we have copy-on-write in PHP.

Beat this :P :

?php

$timeArray = array(/* your string time data */);

function timeStamps($ar) {
  $stamps = array();
  foreach ($ar as $timeString) {
$stamps[strtotime($timeString)] = $timeString;
  }
  return $stamps;
}

function sortTime($ar) {
  $timeStampAr = timeStamps($ar);
  ksort($timeStampAr); // since the keys are integers, timestamps ksort
would do the right thing
  $ar = array_values($timeStampAr);
  return $ar; //dont really need this but just in case someone prefers to
use
  //it differently or needs a copy
}

sortTime($timeArray);

/* this way the strtotime function is only applied once to each time string
which is probably the most expensive. And ksort uses the sort algorithm that
the PHP core programmers regard as the most efficient. I believe quick sort.
CODE NOT TESTED :P might have minor mistakes but i doubt it :P.*/

?
Tim-Hinnerk Heuer

http://www.ihostnz.com
Fred Allen  - California is a fine place to live - if you happen to be an
orange.

2009/2/16 Jochem Maas joc...@iamjochem.com

 Shawn McKenzie schreef:
  Shawn McKenzie wrote:

 ...

  Not tested:

 no shit.

  function time_sort($a, $b)
  {
  if (strtotime($a) == strtotime($b)) {
  return 0;
  }
  return (strtotime($a)  strtotime($b) ? -1 : 1;
  }
 
  usort($time, time_sort);
 
  Well, I just thought, since the strtotime() uses the current timestamp
  to calculate the new timestamp, if you only give it a time then the
  returned timestamp is today's date with the new time you passed.  If you
  had a large array and the callback started at 23:59:59 then you could
  end up with some times from the date it started and some from the next
  day, which of course would not be sorted correctly with respect to times
  only.  So, this might be better (not tested):
 

 ditto (as in nice syntax error).

 
  function time_sort($a, $b)
  {
  static $now = time();
 
  if (strtotime($a, $now) == strtotime($b, $now)) {
  return 0;
  }
  return (strtotime($a, $now)  strtotime($b, $now) ? -1 : 1;
  }
 
 
  Your best bet above.

 and G.W.Bush is a socialist.

 

 I did a little test, the ultra-fast version offered by McKenzie
 is really great as long as your array of time strings aren't ever
 going to be longer than say 3-4 items (with the caveat that you
 don't use a second argument to strtotime() ... if you do then
 even with 3 items it's substantially slower).

 for any reasonable number of items my tests show tedd's version
 pisses on McKenzies from a great height (note that I actually
 optimized Mckenzies variant by halfing the number of calls to
 strtotime()).

 I added a third variant, as a sort of control, which runs pretty
 much on par with tedd's version but uses rather less LOC
 (tedd you might like it as a little example of using array_multisort(),
 i.e. a way of avoiding writing the double foreach loop in this case)

 tedd's variant: sortTime1()
 McKenzie's variant: sortTime2()
 my variant: sortTime3()

 sample output from one of my test runs:
 ===
 ===
 No. of array items = 3, no of iterations = 1
 ---
 timeSort1() ran for 1.306011 seconds
 timeSort2() ran for 1.337358 seconds
 timeSort3() ran for 1.742724 seconds

 No. of array items = 6, no of iterations = 1
 ---
 timeSort1() ran for 2.647697 seconds
 timeSort2() ran for 2.475791 seconds
 timeSort3() ran for 7.268916 seconds

 No. of array items = 9, no of iterations = 1
 ---
 timeSort1() ran for 3.891894 seconds
 timeSort2() ran for 3.960463 seconds
 timeSort3() ran for 18.440713 seconds




 the test script:
 ===
 ===
 ?php
 // TEST
 ini_set(date.timezone, Europe/Amsterdam);

 $iter = 1;
 $time = array(
array(1:30pm, 7:30am, 12:30pm),
array(1:30pm, 7:30am, 12:30pm, 4:45pm, 8:15am, 11:00pm),
array(1:30pm, 7:30am, 12:30pm, 4:45pm, 8:15am, 11:00pm,
 4:30am, 6:45am, 12:00pm),
 );

 foreach ($time as $t)
testIt($t, $iter);


 // FUNCS

 function sortTime1($in_times)
 {
$time = array();
foreach ($in_times as $t)
$time[] = strtotime($t);

sort($time);

$sort_time = array();
foreach ($time as $t)
$sort_time[] = date(g:ia, $t);

return $sort_time;
 }

 function timeSort2($in)
 {
static $time = null;

if (!$time)
$time = time();

$now = array_fill(0, count($in), $time);
$out = array_map(strtotime, $in, $now);
array_multisort($out, SORT_NUMERIC, SORT_ASC, $in);

return $in;
 }

 function timeSort3($a, $b)
 {
static $now = null;

if (!$now)
$now = time();

$a = strtotime($a, $now);
$b = strtotime($b, $now);
if ($a == $b)
return 0;

return $a  $b ? -1 : 1;
 }

 function 

Re: [PHP] Re: Sorting times (SOLVED in half the time, hey tedd get your new and improved variant here)

2009-02-16 Thread Jochem Maas
German Geek schreef:
 Remember we have copy-on-write in PHP.
 
 Beat this :P :

for speed it's way faster, slight issue though, it won't
give the expected output for arrays that contain the same
value more than once. not difficult to fix that,
below a new version of the test script with both your
original variant (timeSort4()) and a version that doesn't mislay
duplicates (timeSort5()). both take half the time to run roughly ... nice :-)

?php


// TEST
ini_set(date.timezone, Europe/Amsterdam);

$iter = 1;
$time = array(
array(1:30pm, 7:30am, 12:30pm),
array(1:30pm, 7:30am, 12:30pm, 4:45pm, 8:15am, 11:00pm),
array(1:30pm, 7:30am, 12:30pm, 4:45pm, 8:15am, 11:00pm, 
4:30am, 6:45am, 12:00pm),
);

foreach ($time as $t)
testIt($t, $iter);


// FUNCS

function sortTime1($in_times)
{
$time = array();
foreach ($in_times as $t)
$time[] = strtotime($t);

sort($time);

$sort_time = array();
foreach ($time as $t)
$sort_time[] = date(g:ia, $t);

return $sort_time;
}

function timeSort2($in)
{
static $time = null;

if (!$time)
$time = time();

$now = array_fill(0, count($in), $time);
$out = array_map(strtotime, $in, $now);
array_multisort($out, SORT_NUMERIC, SORT_ASC, $in);

return $in;
}

function timeSort3($a, $b)
{
static $now = null;

if (!$now)
$now = time();

$a = strtotime($a, $now);
$b = strtotime($b, $now);
if ($a == $b)
return 0;

return $a  $b ? -1 : 1;
}

function timeSort4($ar)
{
$stamps = array();
foreach ($ar as $timeString)
$stamps[strtotime($timeString)] = $timeString;

ksort($stamps);
return array_values($stamps);
}

function timeSort5($ar)
{
$stamps = array();
foreach ($ar as $ts)
if (isset($stamps[$ts]))
$stamps[strtotime($ts)][1]++;
else
$stamps[strtotime($ts)] = array($ts, 1);

ksort($stamps);

$ar = array();
foreach ($stamps as $data)
for ($i = 0; $i  $data[1]; $i++)
$ar[] = $data[0];

return $ar;
}

function testIt($time, $iter)
{
echo \nNo. of array items = , count($time), , no of iterations = 
$iter\n---\n;

$s = microtime(true);
for ($i = 0; $i  $iter; $i++)
timeSort2($time);
$e = microtime(true);
echo timeSort1() ran for .round($e-$s, 6). seconds \n;

$s = microtime(true);
for ($i = 0; $i  $iter; $i++)
timeSort2($time);
$e = microtime(true);
echo timeSort2() ran for .round($e-$s, 6). seconds \n;

$s = microtime(true);
for ($i = 0; $i  $iter; $i++)
usort($time, timeSort3);
$e = microtime(true);
echo timeSort3() ran for .round($e-$s, 6). seconds \n;

$s = microtime(true);
for ($i = 0; $i  $iter; $i++)
timeSort4($time);
$e = microtime(true);
echo timeSort4() ran for .round($e-$s, 6). seconds \n;

$s = microtime(true);
for ($i = 0; $i  $iter; $i++)
timeSort5($time);
$e = microtime(true);
echo timeSort5() ran for .round($e-$s, 6). seconds \n;
}

?


 ?php
 
 $timeArray = array(/* your string time data */);
 
 function timeStamps($ar) {
   $stamps = array();
   foreach ($ar as $timeString) {
 $stamps[strtotime($timeString)] = $timeString;
   }
   return $stamps;
 }
 
 function sortTime($ar) {
   $timeStampAr = timeStamps($ar);
   ksort($timeStampAr); // since the keys are integers, timestamps ksort
 would do the right thing
   $ar = array_values($timeStampAr);
   return $ar; //dont really need this but just in case someone prefers to
 use
   //it differently or needs a copy
 }
 
 sortTime($timeArray);
 
 /* this way the strtotime function is only applied once to each time string
 which is probably the most expensive. And ksort uses the sort algorithm that
 the PHP core programmers regard as the most efficient. I believe quick sort.
 CODE NOT TESTED :P might have minor mistakes but i doubt it :P.*/
 
 ?
 Tim-Hinnerk Heuer
 
 http://www.ihostnz.com
 Fred Allen  - California is a fine place to live - if you happen to be an
 orange.
 
 2009/2/16 Jochem Maas joc...@iamjochem.com
 
 Shawn McKenzie schreef:
 Shawn McKenzie wrote:
 ...

 Not tested:
 no shit.

 function time_sort($a, $b)
 {
 if (strtotime($a) == strtotime($b)) {
 return 0;
 }
 return (strtotime($a)  strtotime($b) ? -1 : 1;
 }

 usort($time, time_sort);

 Well, I just thought, since the strtotime() uses the current timestamp
 to calculate the new timestamp, if you only give it a time then the
 returned timestamp is today's date with the new time you passed.  If you
 had a large array and the callback started at 23:59:59 then you could
 end up with some times from the date it started and some from the next
 day, which of course would not be sorted correctly with respect to times
 only.  So, this might be better (not tested):

 ditto (as in nice syntax error).

 function time_sort($a, $b)
 {
 static $now = time();

 if (strtotime($a, 

Re: [PHP] Re: Sorting times (SOLVED before tedds crappy SOLVED)

2009-02-16 Thread tedd

At 9:56 AM +0100 2/16/09, Jochem Maas wrote:

for any reasonable number of items my tests show tedd's version
pisses on McKenzies from a great height (note that I actually
optimized Mckenzies variant by halfing the number of calls to
strtotime()).


ROTFLOL.   -- I seldom say that!

From a great height!!! Now that's funny!


I added a third variant, as a sort of control, which runs pretty
much on par with tedd's version but uses rather less LOC
(tedd you might like it as a little example of using array_multisort(),
i.e. a way of avoiding writing the double foreach loop in this case)


The speed of the sort doesn't matter at all. The maximum number of 
data that needed to be sorted in my problem would have been 126 (18 
different times for 7 days).


I only presented part of the problem here. It was a distilled version 
of the problem I was working on, which was to allow people to enter 
times that they were available for tutoring in two hour chunks.


So, a person might say they were available from 7:00 am to 9:00 am 
AND also state that they were available from 7:30 am to 9:30 am, 
which was a no-no.


Having two loops allowed me to check after converting to seconds AND 
sorting to see if there were any overlaps. In such case I simply 
deleted the offending data from the array before converting 
everything back into normal time (min:sec).


Re this post -- all I needed was a push in the right direction. I was 
thinking about sorting, but I didn't even consider converting 
everything to seconds and then sorting.


That's really all I needed AND another reason why this list is so 
great! Not only did I get a push in the right direction, but I got a 
good laugh out of it -- from great heights indeed. :-)


Thanks and Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Sorting times (SOLVED before tedds crappy SOLVED)

2009-02-16 Thread Shawn McKenzie
tedd wrote:
 At 9:56 AM +0100 2/16/09, Jochem Maas wrote:
 for any reasonable number of items my tests show tedd's version
 pisses on McKenzies from a great height (note that I actually
 optimized Mckenzies variant by halfing the number of calls to
 strtotime()).
 
 ROTFLOL.   -- I seldom say that!

Haha!  Yes, I was trolling and got a good one from Jochem!  I would say
ROTFLMAO!
 
 From a great height!!! Now that's funny!

Too funny.  I'll have to remember to work this in to some conversation
today at work.  ;-)

 I added a third variant, as a sort of control, which runs pretty
 much on par with tedd's version but uses rather less LOC
 (tedd you might like it as a little example of using array_multisort(),
 i.e. a way of avoiding writing the double foreach loop in this case)
 
 The speed of the sort doesn't matter at all. The maximum number of data
 that needed to be sorted in my problem would have been 126 (18 different
 times for 7 days).
 
 I only presented part of the problem here. It was a distilled version of
 the problem I was working on, which was to allow people to enter times
 that they were available for tutoring in two hour chunks.
 
 So, a person might say they were available from 7:00 am to 9:00 am AND
 also state that they were available from 7:30 am to 9:30 am, which was a
 no-no.
 
 Having two loops allowed me to check after converting to seconds AND
 sorting to see if there were any overlaps. In such case I simply deleted
 the offending data from the array before converting everything back into
 normal time (min:sec).
 
 Re this post -- all I needed was a push in the right direction. I was
 thinking about sorting, but I didn't even consider converting everything
 to seconds and then sorting.
 
 That's really all I needed AND another reason why this list is so great!
 Not only did I get a push in the right direction, but I got a good laugh
 out of it -- from great heights indeed. :-)
 
 Thanks and Cheers,
 
 tedd
 


-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Sorting times (SOLVED before tedds crappy SOLVED)

2009-02-16 Thread tedd

At 9:45 AM -0600 2/16/09, Shawn McKenzie wrote:

tedd wrote:

 At 9:56 AM +0100 2/16/09, Jochem Maas wrote:

 for any reasonable number of items my tests show tedd's version
 pisses on McKenzies from a great height (note that I actually
 optimized Mckenzies variant by halfing the number of calls to
 strtotime()).


 ROTFLOL.   -- I seldom say that!


Haha!  Yes, I was trolling and got a good one from Jochem!  I would say
ROTFLMAO!


 From a great height!!! Now that's funny!


Too funny.  I'll have to remember to work this in to some conversation
today at work.  ;-)



It's good that you take things like that.

I've been hammered by people before and it's always nice to see the 
humor and not take offense.


One of the best insults I ever received was right after the OK 
bombing incident and I was speaking to a group of people and one said 
I wonder how big a crater you'll leave if we doused you down with 
diesel fuel? -- meaning I was full of fertilizer. I almost died 
laughing.


I have several others. :-)

Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Sorting times

2009-02-15 Thread German Geek
The easiest would probably to use
http://nz.php.net/manual/en/function.strnatcmp.php . It would happen to sort
it the right way because am is before pm ;-).

You can of course make it more challenging by converting it into a timestamp
etc. That would be better if you want to sort by date as well etc. If you go
that way you should look at http://nz.php.net/manual/en/function.usort.php .

Regards,
Tim

Tim-Hinnerk Heuer

http://www.ihostnz.com
Alanis Morissette  - We'll love you just the way you are if you're
perfect.

2009/2/15 Shawn McKenzie nos...@mckenzies.net

 Shawn McKenzie wrote:
  tedd wrote:
  Hi gang:
 
  Anyone have/know a routine that will sort an array of times?
 
  For example, a function that would take an array like this:
 
  time[0] ~ '1:30pm'
  time[1] ~ '7:30am'
  time[2] ~ '12:30pm'
 
  and order it to:
 
  time[0] ~ '7:30am'
  time[1] ~ '12:30pm'
  time[2] ~ '1:30pm'
 
 
  Cheers,
 
  tedd
 
 
 
  Not tested:
 
  function time_sort($a, $b)
  {
  if (strtotime($a) == strtotime($b)) {
  return 0;
  }
  return (strtotime($a)  strtotime($b) ? -1 : 1;
  }
 
  usort($time, time_sort);
 
 Well, I just thought, since the strtotime() uses the current timestamp
 to calculate the new timestamp, if you only give it a time then the
 returned timestamp is today's date with the new time you passed.  If you
 had a large array and the callback started at 23:59:59 then you could
 end up with some times from the date it started and some from the next
 day, which of course would not be sorted correctly with respect to times
 only.  So, this might be better (not tested):


 function time_sort($a, $b)
 {
 static $now = time();

if (strtotime($a, $now) == strtotime($b, $now)) {
return 0;
}
return (strtotime($a, $now)  strtotime($b, $now) ? -1 : 1;
 }


 --
 Thanks!
 -Shawn
 http://www.spidean.com

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Re: Sorting times

2009-02-15 Thread Mattias Thorslund

German Geek wrote:

The easiest would probably to use
http://nz.php.net/manual/en/function.strnatcmp.php . It would happen to sort
it the right way because am is before pm ;-).
  



Nope. Unfortunately 12 am (midnight) comes before 1 am, and 12 pm (noon) 
comes before 1 pm. Since you have to account for that, you solution 
won't be as elegant.


Cheers,

Mattias

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Sorting times

2009-02-15 Thread German Geek
Yes, you are right. Hadn't thought about that. But usort is probably better
than making your own sort function because it uses the quick sort algorithm
i believe which is quite efficient. That was the other suggestion...

Tim-Hinnerk Heuer

http://www.ihostnz.com
Fred Allen  - California is a fine place to live - if you happen to be an
orange.

2009/2/16 Mattias Thorslund matt...@thorslund.us

 German Geek wrote:

 The easiest would probably to use
 http://nz.php.net/manual/en/function.strnatcmp.php . It would happen to
 sort
 it the right way because am is before pm ;-).




 Nope. Unfortunately 12 am (midnight) comes before 1 am, and 12 pm (noon)
 comes before 1 pm. Since you have to account for that, you solution won't be
 as elegant.

 Cheers,

 Mattias

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Re: Sorting times (SOLVED before tedds crappy SOLVED)

2009-02-15 Thread Shawn McKenzie
Shawn McKenzie wrote:
 Shawn McKenzie wrote:
 tedd wrote:
 Hi gang:

 Anyone have/know a routine that will sort an array of times?

 For example, a function that would take an array like this:

 time[0] ~ '1:30pm'
 time[1] ~ '7:30am'
 time[2] ~ '12:30pm'

 and order it to:

 time[0] ~ '7:30am'
 time[1] ~ '12:30pm'
 time[2] ~ '1:30pm'


 Cheers,

 tedd


 Not tested:

 function time_sort($a, $b)
 {
 if (strtotime($a) == strtotime($b)) {
 return 0;
 }
 return (strtotime($a)  strtotime($b) ? -1 : 1;
 }

 usort($time, time_sort);

 Well, I just thought, since the strtotime() uses the current timestamp
 to calculate the new timestamp, if you only give it a time then the
 returned timestamp is today's date with the new time you passed.  If you
 had a large array and the callback started at 23:59:59 then you could
 end up with some times from the date it started and some from the next
 day, which of course would not be sorted correctly with respect to times
 only.  So, this might be better (not tested):
 
 
 function time_sort($a, $b)
 {
 static $now = time();
 
 if (strtotime($a, $now) == strtotime($b, $now)) {
 return 0;
 }
 return (strtotime($a, $now)  strtotime($b, $now) ? -1 : 1;
 }
 
 
Your best bet above.

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Sorting times

2009-02-14 Thread Shawn McKenzie
tedd wrote:
 Hi gang:
 
 Anyone have/know a routine that will sort an array of times?
 
 For example, a function that would take an array like this:
 
 time[0] ~ '1:30pm'
 time[1] ~ '7:30am'
 time[2] ~ '12:30pm'
 
 and order it to:
 
 time[0] ~ '7:30am'
 time[1] ~ '12:30pm'
 time[2] ~ '1:30pm'
 
 
 Cheers,
 
 tedd
 
 

Not tested:

function time_sort($a, $b)
{
if (strtotime($a) == strtotime($b)) {
return 0;
}
return (strtotime($a)  strtotime($b) ? -1 : 1;
}

usort($time, time_sort);

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Sorting times

2009-02-14 Thread Shawn McKenzie
Shawn McKenzie wrote:
 tedd wrote:
 Hi gang:

 Anyone have/know a routine that will sort an array of times?

 For example, a function that would take an array like this:

 time[0] ~ '1:30pm'
 time[1] ~ '7:30am'
 time[2] ~ '12:30pm'

 and order it to:

 time[0] ~ '7:30am'
 time[1] ~ '12:30pm'
 time[2] ~ '1:30pm'


 Cheers,

 tedd


 
 Not tested:
 
 function time_sort($a, $b)
 {
 if (strtotime($a) == strtotime($b)) {
 return 0;
 }
 return (strtotime($a)  strtotime($b) ? -1 : 1;
 }
 
 usort($time, time_sort);
 
Well, I just thought, since the strtotime() uses the current timestamp
to calculate the new timestamp, if you only give it a time then the
returned timestamp is today's date with the new time you passed.  If you
had a large array and the callback started at 23:59:59 then you could
end up with some times from the date it started and some from the next
day, which of course would not be sorted correctly with respect to times
only.  So, this might be better (not tested):


function time_sort($a, $b)
{
static $now = time();

if (strtotime($a, $now) == strtotime($b, $now)) {
return 0;
}
return (strtotime($a, $now)  strtotime($b, $now) ? -1 : 1;
}


-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php