c++ - Sorting an STL vector on two values -


how sort stl vector based on 2 different comparison criterias? default sort() function takes single sorter object.

you need combine 2 criteria one. heres example of how you'd sort struct first , second field based on first field, second field.

#include <algorithm>  struct myentry {   int first;   int second; };  bool compare_entry( const myentry & e1, const myentry & e2) {   if( e1.first != e2.first)     return (e1.first < e2.first);   return (e1.second < e2.second); }  int main() {   std::vector<myentry> vec = get_some_entries();   std::sort( vec.begin(), vec.end(), compare_entry ); } 

note: implementation of compare_entry updated use code nawaz.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -