Octobercms - Order Frontend List By Page Variable (variable Not On Database)

- 1 answer

So i'm trying to create a leaderboard with frontend user data on octobercms, the leaderboard will be based on the sum of variables that are currently in the database. I've only recently started to look into twig and october, so please look at this patiently

The general Variable:

{% set totalScore = totalPoints + user.xp + user.progress %}

The List (simplified)

{% for user in activatedUsers %}
<div class="card">
        <p class="name"><span class="rank-title">NAME</span><br>{{ }}</p>
        <p><span class="rank-title">LEVEL</span><br>{{ user.level }}</p>
        <p><span class="rank-title">TOTAL POINTS</span><br>{{ totalPoints }} </p>
        <p><span class="rank-title">PROGRESS</span><br>{{ user.progress }}</p>
        <p><span class="rank-title">XP</span><br>{{ user.xp }}</p>
{% endfor %}

I can't really use the query to sort, since some variables are not in the database, but instead are being defined in the page. I've looked into the sort twig function but i can't get it to work correctly. I've tried this solution, but i get an exception.

I've added the following code in order for the "usort" function to work to the modules/twig/extension file but it did not work, and there has to be a simpler solution...

new \Twig_SimpleFilter('usort', array($this, 'usortFilter'))

public function usortFilter($item){
    usort($item, function ($item1, $item2) {
        if ($item1['orderNo'] == $item2['orderNo']) return 0;
        return $item1['orderNo'] < $item2['orderNo'] ? -1 : 1;

return $item;

I've also tried the logical thing to do after reading the twig documentation:

{% for user in activatedUsers|sort('totalPoints') %}

But no luck there. I can be walking in the wrong paths, so please feel free to suggest another or point out something i've missed. Thanks in advance



Hmm i am not sure where you are setting your activatedUsers but before set that you can simply define this loop to sort them

you should use this only if your dataset is limited in numbers like 10 or 30 entry in list, no more then that its not efficient to sort db records in code

$activatedUsers = <<some code to fetch users>>
$sortedPointsWithUserId = [];
$sortedActivatedUsers = [];

foreach($activatedUsers as $user) {
    // if $totalPoints$sortedActivatedUsers = [];

foreach($activatedUsers as $user) {
    // if $totalPoints  is page variable you can use it directly $this->page['totalPoints'] from components and from page lifecycle $this['totalPoints']
    $sortedPointsWithUserId[$user->id] = $totalPoints + $user->xp + $user->progress;

// sort array in descending order as we are storing totalpoints as value and key as user ID
// arsort maintain key value association  

foreach($sortedPointsWithUserId as $key => $value) {
   $user = $activatedUsers[$key];
   // create variable in model to hold totalPoints so we dont need to calculate again
   $user->totalPoints = $value;
   $sortedActivatedUsers[] = $user;

// $sortedActivatedUsers <-- this is your sorted array and you can use it 

if you have any query please comment