3

I'm trying to order my users with craft.users.order('lastName'), which should be no problem, since I'm pulling that straight from the documentation; however, it's not working for some reason. Is this a current a bug in Craft?

Currently, I have users with these last names: Apple, Campbell, Jackson.

With order('lastName'), it's printing out: Campbell, Jackson, Apple. So no alphabetical order is being applied asc or desc.

5

Values in the documentation list possibilities, like: 'firstName' and 'lastName', which needs to be updated, since Craft only accepts lowercase values.

Changing 'firstName' to 'firstname' solves the issue.

4
  • 2
    Can you point to an example in the docs? – Brad Bell Oct 15 '15 at 18:19
  • I too spent a lot of time attempting to order users by lastName and couldn't figure out why it wasn't working. Fortunately, I found this post and changed the criteria to {order:'lastname'}, which worked. @BradBell The user order docs show the camelCase criteria values as working, but only the lowercase values seem to work in practice. – Aaron Waldon Aug 31 '17 at 13:09
  • 1
    @AaronWaldon weird... the value of order gets passed directly into the SQL query that is generated and mapped back to the specified table columns. MySQL table columns are supposed to be case insensitive, so either should work. Tested locally and it's that way for me. – Brad Bell Aug 31 '17 at 17:59
  • I'm super grateful for this post. I just spent 90 minutes trying to debug my users being sorted arbitrarily, and trying (unsuccessfully) to find some way around it. This is still an issue in Craft 3.3. Thank you! Incidentally, I was debugging the raw MySQL query, and it wasn't sorting by users.firstName.... it was sorting by content.field_firstName—a field I happen to have but whose results in the query (after several joins) were all NULL. – nmjk Jan 30 '20 at 0:09

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.