Note: This is the same issue as reported in http://drupal.org/node/137373 , but since the original author requested a new feature to address the problem and I'm just providing a bug fix I'm filing this separately. Feel free to merge the two issues if desired.

The problem: I have a table view that provides a list of classified ads sorted by creation date and state. I have also allowed the user to sort the results by clicking on the column headers. If the user clicks on the "City" column header she gets results that are sorted by creation date, then by state, then by city. To her, this looks like the sort didn't work - she's expecting them to be sorted primarily by city and probably won't notice if there are any secondary sorts or not.

The solution: Set up the order by clause so that any user-generated sorts precede any sorts that are built in to the view. The way to do this is to change line 638 of views_query.inc (v 1.51.2.11) from:

$this->orderby[] = str_replace('ORDER BY ', '', $result);

to:

array_unshift($this->orderby, str_replace('ORDER BY ', '', $result));

This prepends the user-generated sorts rather than appending them and solves the problem. (Of course it may create new ones - so far I haven't found any, but please let me know if you think of some. I'm no guru when it comes to Views internals.)

I'm happy to provide a patch if you let me know which branch(es) you'd like it rolled against. (For the record, I took a quick glance at the Views 2 code for Drupal 6 and this problem seems to exist there, too.)

Comments

jnvsor’s picture

I found an error, the code works perfectly, but seeing as even though the default sort is off, it sorts by its default, It now never sorts by the sort criteria...

merlinofchaos’s picture

Status: Active » Closed (won't fix)

This will not be fixed in Views 1.