What I have:

A php custom field that returns directly a number (2, 3, 0, 1 ...).

What I want: sort by that field.

But I don't know exactly how to, whatever I do in the sort php field doesn't change the overall result.

Thanks in advance.

Kind regards.

Comments

infojunkie’s picture

In the Sort code section of the Sort field, you need to implement a comparison function for your custom field. The code should look something like this:

return $row1->nid < $row2->nid ? -1 : (int)$row1->nid > $row2->nid;

Basically, the function should return -1, 0, 1 depending on whether the field value in $row1 is less than, equal, or greater than the value in $row2.

To find the name of your custom field, you can prepend something like dsm($row1); to the code above (with Devel module enabled).

thedavidmeister’s picture

this is actually an invaluable little nugget of an explanation, although IMO using the ternary operator with nested comparisons makes the example unnecessarily hard to follow when you're aiming to make things *more* obvious for early adopters :P

below is how i got my gig guide, that used UNIX timestamps, to sort using only "day granularity" (since i was grouping the display by day) and then sort each day alphabetically by node title.

NOTE: without views_php, i had no way of changing the granularity of my date fields in the sort. Thanks for the great module!

//from views_php:
//The comparison code must return an integer less than, equal to, or greater than zero 
//if the first row should respectively appear before, stay where it was compared to, or appear after the second row.

//what we're doing:
//if row1's date is less than row2's date, row1 should appear before row2 => return a negative integer
//if row1's date is equal to row2's date then we compare node titles alphabetically
//ir row1's date is greater than row2's date, row1 should appear after row2 => return a positive integer

//convert timestamp into a timestamp "truncated" to midnight on previous day
$row1_daystamp = strtotime('Y-m-d\TH:i:sO', date('Y-m-d\T00:00:00O', $row1->field_datestamp_gig_value));
$row2_daystamp = strtotime('Y-m-d\TH:i:sO', date('Y-m-d\T00:00:00O', $row2->field_datestamp_gig_value));

//hopefully no title comparison is necessary..
$comparison = $row1_daystamp - $row2_daystamp;

//but if it is..
if ($comparison == 0) {
	$comparison = strcmp($row1->title, $row2->title);
}

return $comparison;

EDIT: did some further testing, this doesn't quite do what i expected.. will get back to you with more refined code when i figure out how this is all supposed to work.

thedavidmeister’s picture

ah, i totally munged the way arguments should be passed to strtotime.

this actually works:

//from views_php:
//The comparison code must return an integer less than, equal to, or greater than zero
//if the first row should respectively appear before, stay where it was compared to, or appear after the second row.

//what we're doing:
//if row1's date is less than row2's date, row1 should appear before row2 => return a negative integer
//if row1's date is equal to row2's date then we compare node titles alphabetically
//ir row1's date is greater than row2's date, row1 should appear after row2 => return a positive integer

//convert timestamp into a timestamp "truncated" to midnight on previous day
$row1_daystamp = strtotime(date('Y-m-d\T00:00:00O', $row1->field_datestamp_gig_value));
$row2_daystamp = strtotime(date('Y-m-d\T00:00:00O', $row2->field_datestamp_gig_value));

//hopefully no title comparison is necessary..
$comparison = $row1_daystamp - $row2_daystamp;

//but if it is..
if ($comparison == 0) {
    $comparison = strcmp($row1->title, $row2->title);
}

return $comparison;
infojunkie’s picture

Status: Active » Closed (fixed)

@thedavidmeister, thanks for the expanded explanation. Marking as fixed.

thedavidmeister’s picture

well it is only half fixed really.. there's still no README.txt file provided with the project.

i'll open a separate issue for that though.

grasmash’s picture

Status: Closed (fixed) » Active

So it seems that if you use a Views PHP field as a sort handler, subsequent (views-generated) sort handlers do not work correctly? I'm assuming that's why you had to run strcmp() rather than simply adding a normal views title sort handler.

Is this true? If so, it needs to be noted in the help text for the sort handler.

Liam Morland’s picture

Issue summary: View changes
Status: Active » Closed (outdated)

Drupal 6 is no longer supported. If this applies to a later version, please re-open and update the version.