我们有一个如下的“包装”集合:
<?php
namespace App\Policies;
use App\Post;
use App\User;
class PostPolicy
{
/**
* Determine if the given post can be updated by the user.
*
* @param \App\User $user
* @param \App\Post $post
* @return bool
*/
public function update(User $user, Post $post)
{
return $user->id === $post->user_id;
}
}
现在,我们需要找到最高版本。所以我们写下代码:
| package | version | released |
| --------------------------- | ------- | -------- |
| 'https://example.com/a.pkg' | '1.0.0' | true |
| 'https://example.com/b.pkg' | '2.0.0' | true |
| 'https://example.com/c.pkg' | '3.0.0' | true |
| 'https://example.com/d.pkg' | '4.0.0' | true |
但是很快,我们发现它不起作用。由于find({ released: true }).sort({ version: -1 }).limit(1)
使用字符串比较,因此这不是我们期望的。我们期望的是版本比较,这是一个自定义函数(javascript)。
最后,问题是:如何编写自定义的比较器函数?