基于多个值类型对矢量进行排序

Sorting a vector based on multiple value types

本文关键字:排序 于多个 类型      更新时间:2023-10-16

我想根据多个值对自定义结构进行排序。。。我已经处理过一些情况,其中我必须使用<>按2个不同的数字进行排序在自定义排序方法中。。。除了这次我必须按照2个布尔值和一个数字进行排序。

struct MyStruct
{
B1 = false;
B2 = true;
N1 = 0;
}

现在。。。我希望它排序如下。。。

任何B1和B2为真的东西都是第一位的。任何有B1的都是下一个。

这两个部分也需要按N1(较低=先到)进行排序

所以B1和B2需要从N1的最低到最高排序。紧随其后的是B1,N1从最低到最高。紧随其后的是从最低到最高的顺序。

这是我迄今为止的尝试。。。

    if (lhs.boss && lhs.isLOS && lhs.distancetome < rhs.distancetome)
        return true;
    if (lhs.boss && lhs.isLOS && lhs.distancetome > rhs.distancetome)
        return false;
    if (lhs.boss && lhs.distancetome < rhs.distancetome)
        return true;
    if (lhs.boss && lhs.distancetome > rhs.distancetome)
        return false;
    if (lhs.distancetome < rhs.distancetome)
        return true;
    return false;

类似的东西:

struct complex_comparison
{
    bool operator()(const MyStruct& lhs, const MyStruct& rhs)
    {
        return lhs.B1 && lhs.B2 != rhs.B1 && rhs.B2 ? lhs.B1 && lhs.B2 > rhs.B1 && rhs.B2 :
                                   lhs.B1 != rhs.B1 ? lhs.B1 > rhs.B1 :
                                   lhs.N1 < rhs.N1;
    }
};

示例:http://coliru.stacked-crooked.com/a/b4888ac34d2ca6bb