You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于优先级输入排序Tournament对象列表

How to Sort Tournament List by Platform Priority (Ps4 → Xbox → Pc) in Pimcore

Got it, let's work through this sorting challenge together. The core issue here is that we can't rely on fixed platform IDs for sorting, so we need to tie the custom priority values you added to the Platform objects directly to your Tournament list. Here's a step-by-step solution:

First, Build a Platform Priority Map

First, we'll create a quick lookup array that maps each platform ID to its priority value. This avoids repeatedly searching through platform objects during sorting, which keeps things efficient:

// Fetch all platform objects and build a ID → priority map
$platformList = new Platform\Listing();
$platformObjects = $platformList->getObjects();

$platformPriorityMap = [];
foreach ($platformObjects as $platform) {
    $platformPriorityMap[$platform->getId()] = $platform->getPriority();
}

// Set a default priority for any unexpected platforms (they'll sort last)
$defaultPriority = 4;

Then, Sort Your Tournament List with Custom Logic

Instead of relying solely on Pimcore's Listing setOrderKey (which can't handle custom priority values easily), we'll fetch the tournaments (pre-sorted by the first two conditions via the database to reduce work), then use uasort to apply the platform priority sorting:

// Fetch tournaments pre-sorted by start and game__id via database
$tournamentList = new Tournament\Listing();
$tournamentList->setCondition('start >= unix_timestamp(NOW() - INTERVAL 1 DAY)');
$tournamentList->setOrder('ASC');
$tournamentList->setOrderKey(['start', 'game__id']);
$tournaments = $tournamentList->getObjects();

// Apply custom sorting for platform priority
uasort($tournaments, function ($tourneyA, $tourneyB) use ($platformPriorityMap, $defaultPriority) {
    // 1. First sort by start timestamp (ASC)
    $startComparison = $tourneyA->getStart() - $tourneyB->getStart();
    if ($startComparison !== 0) {
        return $startComparison;
    }

    // 2. If start times match, sort by game__id (ASC)
    $gameIdComparison = $tourneyA->getGame()->getId() - $tourneyB->getGame()->getId();
    if ($gameIdComparison !== 0) {
        return $gameIdComparison;
    }

    // 3. If games match, sort by platform priority (ASC: Ps4(1) → Xbox(2) → Pc(3))
    $priorityA = $platformPriorityMap[$tourneyA->getPlatform()->getId()] ?? $defaultPriority;
    $priorityB = $platformPriorityMap[$tourneyB->getPlatform()->getId()] ?? $defaultPriority;

    return $priorityA <=> $priorityB; // Spaceship operator for clean comparison
});

Key Notes

  • Why the priority map? It makes sorting faster—we don't have to loop through all platform objects every time we compare two tournaments.
  • Default priority: The $defaultPriority ensures any tournaments linked to platforms without a set priority won't break the sort, and they'll end up at the bottom of the list.
  • Spaceship operator: The <=> operator simplifies the priority comparison—it returns -1, 0, or 1 depending on which value is larger, which is perfect for sorting callbacks.
  • Pre-sort via database: We let the database handle the first two sorting conditions (start and game__id) because database sorting is more efficient than doing everything in memory.

Just make sure your content team sets the priority values correctly: Ps4 = 1, Xbox = 2, Pc = 3. The ascending sort will then enforce your desired platform order.

内容的提问来源于stack exchange,提问作者vprgj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 14:27:34