c++ stl std::sort使用例子

发布时间 2023-08-25 10:20:07作者: 悉野
class User
{
public:
    int32_t m_fight_power;
private:
    int32_t m_level;
};

bool CenterData::compare(const User *left, const User *right)
{
    if(left->m_fight_power != right->m_fight_power)
    {
        return left->m_fight_power > right->m_fight_power;
    }
    return false;
}

typedef std::vector<User*> UserVector;
UserVector userVector;
std::sort(userVector.begin(), userVector.end(), compare);
struct DamageData
{
    EntityUUID player_id;
    int64_t damage;
    std::string player_name;
};
bool compareDamage(const std::shared_ptr<DamageData> left, const std::shared_ptr<DamageData> right)
{
    if (left->damage != right->damage)
    {
        return (left->damage > right->damage);
    }
    return false;
}
vector<std::shared_ptr<DamageData>> ranks;
std::sort(ranks.begin(), ranks.end(), compareDamage);