参考:
https://blog.csdn.net/u013346007/article/details/81877755
https://www.linuxidc.com/Linux/2017-01/140078.htm
https://www.cnblogs.com/gccbuaa/p/7111480.html
sort和stable_sort都是全排序函数,但是sort是非稳定排序算法,而stable_sort是稳定排序算法。
#include "paixu.h" #include <qstring.h> struct MyStruct { int id; QString name; }; using namespace std; bool bigger_than(const MyStruct& s1, MyStruct& s2) { return (s1.id > s2.id); } bool little_than(const MyStruct& s1, MyStruct& s2) { return (s1.id <= s2.id); } paixu::paixu(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); QList<MyStruct> m_list; MyStruct ms1; ms1.id = 10; ms1.name = "10"; MyStruct ms2; ms2.id = 110; ms2.name = "110"; MyStruct ms3; ms3.id = 100; ms3.name = "100"; MyStruct ms4; ms4.id = 120; ms4.name = "120"; MyStruct ms5; ms5.id = 104; ms5.name = "104"; MyStruct ms6; ms6.id = 108; ms6.name = "108"; MyStruct ms7; ms7.id = 109; ms7.name = "109"; m_list.append(ms1); m_list.append(ms2); m_list.append(ms3); m_list.append(ms4); m_list.append(ms5); m_list.append(ms6); m_list.append(ms7); std::sort(m_list.begin(), m_list.end(), bigger_than); int a = 100; std::sort(m_list.begin(), m_list.end(), little_than); a = 100; } paixu::~paixu() { }