Can I sort users by a custom attibute?

Permalink 4 users found helpful
Hello.

I'm setting up a page that displays all members of a particular user group. So far so good, but I can only seem to sort by the basic attributes and not custom ones. The UserList->sortBy() only seems to offer that. Instead, I want to sort by a custom user attribute, so that I can sort by the person's actual full name and not username.

Is this possible? Thanks.

 
jgarcia replied on at Permalink Reply
jgarcia
Did you find a solution to this? Anyone else have a solution? I have a single page with a UserList object and I need to sort the users by a particular attribute. Any thoughts?
jgarcia replied on at Permalink Reply
jgarcia
Okay, so this is definitely possible, because you can do it on the backend on the users page. I'm going to dig into the code and will post what I find.
jgarcia replied on at Permalink Best Answer Reply
jgarcia
It was much easier than I expected...you just have to prefix your first "sortBy" parameter with "ak_" and then the attribute handle. Here's an example:

$userList = new UserList(); 
$userList->sortBy('ak_last_name', 'asc');


Pretty easy :)
sebastienj replied on at Permalink Reply
sebastienj
That's REALLY great !!!!
shotrox replied on at Permalink Reply
shotrox
Hi

is this different in the newest version? I need to sort a few hundred users by lastname and my code does not seem to work.

This is on my members.php

<?php   
      $av = Loader::helper('concrete/avatar');
      Loader::model('user_list');
      $ul = new UserList();
      $ul->filterByGroup('Society Members');
      $ul->sortBy('ak_lastname', 'asc');
      $ul->setItemsPerPage( 1000 );
foreach($users as $user) { 
      $joindate = $user->getAttribute('joindate');
      $lastname = $user->getAttribute('lastname');
      $firstname = $user->getAttribute('firstname');
      $middleinit = $user->getAttribute('middleinit');
      $spouse = $user->getAttribute('spouse');
      $title = $user->getAttribute('title');
      $officer = $user->getAttribute('officer');


Can you help me? Thanks :-)
cinq replied on at Permalink Reply
cinq
Hey Shotrox -

Did you ever get this working?