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

javascript - Iterate over array and calculate average values of array-parts -

ASP.NET Javascript: window.open won't work twice -

jquery - Opera does not change the height of the page. Why? -