digitex

Confusing Alphabetical sorting behaviour

Recommended Posts

I have a list of users that I want to output in a paginated directory in alphabetical order by last_name. When I first wrote it, my selector looked like this:

$members = $pages->find("template=user, roles=member, limit=100");

it sorted correctly as follows:

  • AGLA, H
  • AL, J
  • AL, L
  • BAILEY, M
 
So then I added a sort to ensure it lists by last_name:
$members = $pages->find("template=user, roles=member, limit=100, sort=last_name");

The resulting order looks like this:

  • HALLS, T
  • WEBSTER, T
  • AGLA, H
  • AL, J
  • AL, L
  • BAILEY, M
 
The obvious question is why is Halls and Webster coming before Agla? Can anyone explain it for me? I want to add a filter to allow sorting A-Z and Z-A so I need to include a sort in the selector but I need to get it to sort correctly first.

Share this post


Link to post
Share on other sites

@digitex

Are you sure that you don't have space character before H and W letters? 

  • Like 1

Share this post


Link to post
Share on other sites

Processwire Sort using PHP "SORT_REGULAR", not "SORT_NATURAL"

If you have a string with a upper-case first character, and another one with lowercase first char, then theres a sorting Problem... 😉 

Are your Items all upper-case or mixed?

Example:

- aSDF
- bSDF
- Bsdf

is sorted like this:

- Bsdf
- aSDF
- bSDF

  • Like 2

Share this post


Link to post
Share on other sites
14 hours ago, Zeka said:

@digitex

Are you sure that you don't have space character before H and W letters? 

That's the first thing I checked. I even cleared the field and re-entered the names manually. That's not it.

Share this post


Link to post
Share on other sites
2 hours ago, zoeck said:

Processwire Sort using PHP "SORT_REGULAR", not "SORT_NATURAL"

If you have a string with a upper-case first character, and another one with lowercase first char, then theres a sorting Problem... 😉 

Are your Items all upper-case or mixed?

No the last names are all uppercase. Members were imported from CSV and they were like that on import. It's good to know though.

I switched the selector to title instead of last_name and it sorted properly. The only difference I can see between the 2 fields is title is lowercase so I wrote a function to convert all the last_name values to title case and it seems to be working now.

I still think it's odd behaviour that uppercase should have sorting issues. Thanks to Zoeck for the insight.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.