Jump to content

[SOLVED] strcmp for integers?


Rebelrebellious

Recommended Posts

function orderBy($data, $field){
if($field == 'time'){
	$code = "return strcmp(strtotime(\$a['$field']), strtotime(\$b['$field']));";
} else {
	$code = "return strnatcmp(\$a['$field'], \$b['$field']);";
}
usort($data, create_function('$a,$b', $code));
return $data;
}
$array = orderBy($array, $sortby);

 

The goal is to sort my time columns by the datetime which is formatted '2008-12-29 13:34:30' . Do I need to call strtotime() on each one? In other words, can I just sort the string? Either way, what is wrong with this because the results are not returning in  asorted fashion? Thank you.

Link to comment
Share on other sites

This is the result order that I am getting.

Time Agent Location Event

2008-12-29 13:58:38 413 19 Call ended.

2008-12-29 13:06:22 413 19 New response: oh yeah.

Question: are you happy?

2008-12-29 13:09:26 413 15 New response: fish sticks

Question: what would make you happier?

2008-12-29 13:58:40 413 15 Call ended.

2008-12-29 13:11:54 413 19 Call ended.

2008-12-29 13:09:12 413 15 Call placed.

2008-12-29 13:11:56 413 19 Call placed.

2008-12-29 12:56:19 413 1 Call ended.

2008-12-29 12:56:19 413 19 Call placed.

Link to comment
Share on other sites

The solution I settled for uses the else clause for times and for other columns. I have discarded the strtotime conversion. My thinking is that the strnatcmp is better than doing strtotime. I really don't know however.

 

The reason I have chosen to sort the results with php is that the array i am sorting is built from a compilation of three seperate queries. Is it possible to execute three queries in a row but return one result set that contains the reults from all three queries?

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.