Advertisement
Vultraz

Untitled

Aug 27th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. void lobby_info::sort_users(bool by_name, bool by_relation)
  2. {
  3.     users_sorted_.clear();
  4.     for(auto& u : users_) {
  5.         users_sorted_.push_back(&u);
  6.     }
  7.  
  8.     std::sort(users_sorted_.begin(), users_sorted_.end(), [&](const user_info* u1, const user_info* u2) {
  9.         if(by_name && by_relation) {
  10.             return u1->relation < u2->relation || (u1->relation == u2->relation && u1->name < u2->name);
  11.         }
  12.  
  13.         return (by_name ? u1->name < u2->name : true) && (by_relation ? u1->relation < u2->relation : true);
  14.     });
  15. }
  16. ======================================================
  17.  
  18. struct user_sorter_name
  19. {
  20.     bool operator()(const user_info& u1, const user_info& u2)
  21.     {
  22.         return u1.name < u2.name;
  23.     }
  24.     bool operator()(const user_info* u1, const user_info* u2)
  25.     {
  26.         return operator()(*u1, *u2);
  27.     }
  28. };
  29.  
  30. struct user_sorter_relation
  31. {
  32.     bool operator()(const user_info& u1, const user_info& u2)
  33.     {
  34.         return static_cast<int>(u1.relation) < static_cast<int>(u2.relation);
  35.     }
  36.     bool operator()(const user_info* u1, const user_info* u2)
  37.     {
  38.         return operator()(*u1, *u2);
  39.     }
  40. };
  41.  
  42. struct user_sorter_relation_name
  43. {
  44.     bool operator()(const user_info& u1, const user_info& u2)
  45.     {
  46.         return static_cast<int>(u1.relation) < static_cast<int>(u2.relation)
  47.                || (u1.relation == u2.relation && u1.name < u2.name);
  48.     }
  49.     bool operator()(const user_info* u1, const user_info* u2)
  50.     {
  51.         return operator()(*u1, *u2);
  52.     }
  53. };
  54.  
  55. void lobby_info::sort_users(bool by_name, bool by_relation)
  56. {
  57.     users_sorted_.clear();
  58.     for(auto & u : users_)
  59.     {
  60.         users_sorted_.push_back(&u);
  61.     }
  62.     if(by_name) {
  63.         if(by_relation) {
  64.             std::sort(users_sorted_.begin(),
  65.                       users_sorted_.end(),
  66.                       user_sorter_relation_name());
  67.         } else {
  68.             std::sort(users_sorted_.begin(),
  69.                       users_sorted_.end(),
  70.                       user_sorter_name());
  71.         }
  72.     } else if(by_relation) {
  73.         std::sort(users_sorted_.begin(),
  74.                   users_sorted_.end(),
  75.                   user_sorter_relation());
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement