0

Drupal 7.18 w/ Views 7.x-3.8 user here.

I have a view set up that consists of rows of objects, which have names and 'yes/no' properties. By default, it is sorted alphabetically by object name. I want to allow the user to be able to sort through the view using the various properties, but to then have those sorted results be alphabetized for them post-hoc. Currently, when the user sorts through the view using property X (with either 'yes' or 'no' content), the objects seem to be retrieved based on their location in the database table, or some other factor. Either way, they are in no way alphabetical which means that this view of 500+ results is impractical to sort in this fashion.

So, is it possible to set up a sort mechanism where the results are (1) sorted by property into their respective 'yes'/'no' groups (or any other groups with identical content for that matter) and then (2) alphabetized?

To demonstrate what I mean, I currently have something like this:

NAME    PROPERTYX
Alpha   yes
Beta    no
Delta   yes
Epsilon no
Gamma   yes

Which when sorted using PROPERTYX (let's say descending), might return something like:

NAME    PROPERTYX
Delta   yes
Alpha   yes
Gamma   yes
Epsilon no
Beta    no

(As I said, I suppose this random order is based on the object's actual location in the database table.)

When the desired output would be:

NAME    PROPERTYX
Alpha   yes
Delta   yes
Gamma   yes
Beta    no
Epsilon no

Can this be accomplished somehow?

1

It is definitely be possible to add a second sort to the query which will give the desired effect, but you will have to write some custom code.

There are several hooks that Views provides that you can use in a custom module. I suggest looking at hook_views_query_alter(). The example code on the API page actually shows it altering a WHERE clause, but creative use of dsm()'s will help you figure out what you need to change.

I'm sorry that I can't give you code (because this kind of modification is highly specific to your installation), but this is definitely the way to go.

1

If I understand correctly, you want to sort by PROPERTYX first, and then alphabetically sort the items with the same PROPERTYX value - is that correct?

You can do that by adding multiple sort criteria. Make sure that the "Sort by PROPERTYX (desc)" criterium comes first and the "Sort bt title (asc)" comes second.

  • Yes! That did the trick. By placing the "sort by title (asc)" criterium at the very end, the items of all of the preceding sorts with the same value are now alphabetized. Thank you! – astrosa Jul 11 '14 at 17:05
  • Although -- if the title is the first item in the sort menu, this now means that it is now not possible to sort by title descending. The "sort by title (asc)" criterium at the end overrides that. – astrosa Jul 11 '14 at 18:27
  • Of course you should not add two sort criteria on the same field or property, that wouldn't make sense. Just add (or rearrange) the sort criteria in the right order, and remove the ones you don't need. – marcvangend Jul 11 '14 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.