Hi,
I'm using a Global: math expression field to calculate a score from other fields.
I would like to sort the rows by the math expression score, using this Sort Code I found here: http://drupal.org/node/1145656

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

But it doesn't work, any help?

Thank you in advance

Comments

infojunkie’s picture

Insert dsm($row1); before the return statement. Is your expression field correct?

Anonymous’s picture

Thank you for your answer,
The output is expression (NULL) on every row, but the preview displays the score ok.

infojunkie’s picture

That's your bug. Are you using a Global: PHP field for expression? In that case, make sure you have the value computed in the "Value code" area of the field settings.

Yanxi’s picture

I'm having the same problem. After insetting dsm($row1); I get a result of each Global: PHP field as (NULL). But they display the correct result in the preview. I've set the correct value in the "Value code" area as well. Just can't make the sorting work!

infojunkie’s picture

@Yanxi: You're probably not using the latest Views PHP dev version. Please update and let us know.

Yanxi’s picture

Thanks for your prompt reply infojunkie. I actually downloaded it yesterday. I used print_r($data) and it clearly shows the correct result. But when I used dsm($row1); only the non-php fields' correct results are displayed. All the Global: PHP fields are (NULL). Bizarre!

infojunkie’s picture

Status: Active » Closed (won't fix)

I took a look at the code for Global: Math expression field. Unfortunately, it doesn't compute its value until rendering time, which means it cannot be used for sorting purposes. Because this handler is part of Views core, nothing much can be done here. Sorry!

Yanxi’s picture

I might be missing something. I use 3 Global:PHP fields in my view. The first two are using some sql query to return voting results (number) from the database. The third one is to sum the first two's results. I didn't see Global: Math expression field. Are they the same? I don't know what this Math expression field refers to. I did a bit search on drupal site and didn't find any information about it.

infojunkie’s picture

@Yanxi: the Global: Math expression field is supplied by Views core, not by Views PHP. That's why I suspect you're dealing with a different issue.

Yanxi’s picture

Sorry, my fault. So should I open a new issue? My problem is quite similar to the original post but I am not using Global: Math expression field.

infojunkie’s picture

Yes please.

Yanxi’s picture

I opened a new issue #1178170: Cant sort