C++ STL按变量排序,然后按升序排序

C++ STL按变量排序,然后按升序排序,c++,sorting,stl,C++,Sorting,Stl,如何按字母顺序排序,然后按类中的int变量排序?如何组合它们,以便如果计数器相同,它将按字母顺序返回 // sort pages by its popularity bool popularity(const Page &a, const Page &b) { return a.counter > b.counter; } // alphabetical sort bool alphaSort(const Page &a, const Page &b

如何按字母顺序排序,然后按类中的
int
变量排序?如何组合它们,以便如果
计数器
相同,它将按字母顺序返回

// sort pages by its popularity

bool popularity(const Page &a, const Page &b) {
 return a.counter > b.counter;
}

// alphabetical sort

bool alphaSort(const Page &a, const Page &b) {
    return a.name < b.name;
}
// Combination?
sort(.begin(), .end(), ??)
//按流行程度对页面进行排序
bool受欢迎程度(常数页和a、常数页和b){
返回a.计数器>b.计数器;
}
//字母排序
布尔字母排序(常数页和a、常数页和b){
返回a.name
将您的两个标准扩展到:

bool组合排序(常量页和a、常量页和b)
{
返回a.counter>b.counter | |(a.counter==b.counter&&a.name
使用上述组合排序功能(根据需要先修改字母顺序,然后修改int值),可以调用如下排序:

假设你有一个页面向量

bool combinedSort(const Page &a, const Page &b)
{
    return a.name < b.name || (a.name == b.name && a.counter < b.counter);
}

std::vector<Page> pages;
std::sort(pages.begin(), pages.end(), combinedSort);
bool组合排序(常量页和a、常量页和b)
{
返回a.name
您的意思是要按人气对页面进行排序,然后按字母顺序对人气相同的页面进行排序吗?是的,这正是我想要实现的。这两个函数都可以工作,只是不确定如何将它们组合在一起。或者,您可以先进行排序,然后进行稳定排序。条件可以重写为
return std::tie(b.counter,a.name)
@Tim Seguine:这可能在某种程度上起作用,但您不能将
alphaSort
传递给稳定排序例程。它将重新排列您以前的顺序(除非两个条件匹配生成的分区偶然发生)。@Jarod42:谢谢您的评论,这确实有点好——一旦您愿意对@davidhigh no进行额外的间接排序,@davidhigh no,您将
alphaSort
首先进行稳定排序,然后进行
普及度
。稳定排序不会改变具有相同流行度的项目的相对顺序,这意味着它们仍然是按字母顺序排序的。请注意,
a.name
不是OP在他的问题中所要求的,尽管考虑到他的接受度,这似乎是他想要的。
bool combinedSort(const Page &a, const Page &b)
{
    return a.name < b.name || (a.name == b.name && a.counter < b.counter);
}

std::vector<Page> pages;
std::sort(pages.begin(), pages.end(), combinedSort);