D7 sort a view by a select list weight

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
gadams's picture

Hi,

I'm working on this site: http://oma.wcltech.com

I've got a site with a Content Type of newsletter with a select list called seasons.
The idea is to build a view that sorts the items in the order:
Seasons should be in Winter, Spring, Summer, Autumn order.

Any idea on how to accomplish this?

Comments

your answer is within your question

greta_drupal's picture

Unless I am not understanding your question, your answer is within your question.

Use the Views "Sort Criteria" field. Or, you can use "group by", but you will have to Show your view by "fields" rather than "node".

You didn't state which version of Drupal and Views you are using. But, those options go pretty far back for Views.

He did, "D7" is in the title

JCL324's picture

He did, "D7" is in the title :-)

JCL

G, in your select text field,

JCL324's picture

G, in your select text field, prepend a number in front of the stored data:

Winter|1-Winter
Spring|2-Spring
Summer|3-Summer
Autumn|4-Autumn

Then you can sort on that field.

JCL

My bad (re: Drupal version).

greta_drupal's picture

My bad (re: Drupal version). I'd say even better than prepending the field data, make that field a taxonomy field -- if not already. Then create your season taxonomy and give a weight to the terms there. Views should honor (by design, whether fully implemented yet).

Views doesn't inherit the

gadams's picture

Views doesn't inherit the weight.

I'll write a plugin, it'll probably take me some time.

G

"Just as diamonds are forever, Google Cache is too"

Did you write the Plugin?

mpisano's picture

It'll be very usefull!
Thanks.

Martin

Any progress?

jwillers's picture

Just curious if you wrote a plugin or used another work around. I have a similar situation that I want to sort by a list, and I do imports via Feeds, so I don't believe that adding indexes on the list would help very much.

Thanks in advance!

Joel

Module for Sorting views

sbgranny's picture

I'm a total newbie who is working on converting a site I did in Dreamweaver to Drupal 7. Had an issue with sorting views and found the Draggableviews module which allows an administrator or user given permission to drag rows of a table view to any order they wish.

Three ways to do this

tanius's picture

I found three options for doing this, my preferred one first:

Use hook_views_query_alter. For defining an arbitrary sort order in views (also for list-type fields like "status", "priority" etc.), there is quite an elegant solution with hook_views_query_alter(): MPD's drupal.stackexchange.com answer for "How do you define an arbitrary sort sequence for views?"

Use numeric prefixes. The idea with using a numeric prefix to field values works, too (see) but is maintenance intensive if you have to add more values later or change their sort order.

Use taxonomy term weight. Another option (that I did not test though) is to make the field a taxonomy term field, and sort by taxonomy term weight. This becomes possible (without the custom module proposed above) by creating a relation to the taxonomy in the view, which makes the term weight available in the view's sort criteria) [source] . While this works, for large data sets this should be slower than the first two solutions above, since it needs more joins in the database. (But there's the option of view caching of course ...)

Florida

Group organizers

Group categories

Florida Topics

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: