【发布时间】:2011-03-18 01:25:43
【问题描述】:
我需要为具有多重比较的论坛程序排序对象数组。每个帖子(主线程或回复)都是 Post 类的一个实例。我需要通过它们的 timeLastChanged 属性(用于碰撞)对主线程进行排序,然后通过它们的 timeStarted 属性(最旧的在前)对它们下面的回复进行排序。我的目标是按照将在论坛表中显示的确切顺序对数组进行排序。简单来说,排序和显示如下:
- Main thread
- Reply
- Reply
- Reply
- Reply
- Reply
- Reply
- Main thread
等
我看过 usort 等,但我不知道比较函数是如何工作的。哦,每个对象都有一个属性 isReply,如果它不是回复,则设置为 0,如果是回复,则设置为回复的帖子的线程 ID。每个对象都有一个属性 TID,它是它的线程 ID(唯一)。我还有一个名为 numReplies 的属性,它显示一条消息有多少直接回复(然后,当然,回复会有自己的 numReplies 显示有多少回复分配给该回复等)。
我已经关闭了显示部分并从数据库中获取信息,我只是不确定最有效的排序方式。感谢您的帮助。
编辑:这是我启动类的代码,我排除了我从数据库中获取数据的部分,但只知道所有适用的 Post 实例都分配给它们各自的对象数组(回复是在replyObjs中,电源在mainsObjs等):
class Category {
public $title;
public $majCatID;
public $catID;
public $modLevel;
public $postingLevel;
public $hostedByUID;
public $order;
public $shortName;
public $info;
public $stickyObjs;
public $mainsObjs;
public $replyObjs;
public $orderedObjs;
private $database;
public function __construct($catID) {
$this->database = new TBDatabase;
$result = $this->database->getIntRow(CAT_TABLE, 'catID', $catID);
$row = $this->database->fetchAssoc($result);
$this->title = $row['title'];
$this->majCatID = $row['majCatID'];
$this->catID = $row['catID'];
$this->modLevel = $row['modLevel'];
$this->postingLevel = $row['postingLevel'];
$this->hostedByUID = $row['hostedByUID'];
$this->order = $row['order'];
$this->shortName = $row['shortName'];
$this->info = $row['info'];
}
public function sortPosts() {
$table = $this->shortName."_threads";
$this->getStickyObjs();
$numStickies = count($this->stickyObjs);
$this->getMainObjs();
$numMains = count($this->mainsObjs);
$this->getReplyObjs();
$numReplies = count($this->replyObjs);
}
【问题讨论】:
标签: php arrays sorting object forum