Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Ben1980
Last active February 22, 2019 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ben1980/29aac691f67fc48deff8e62cfbb95588 to your computer and use it in GitHub Desktop.
Save Ben1980/29aac691f67fc48deff8e62cfbb95588 to your computer and use it in GitHub Desktop.
std::vector<Complex> vec = { { 3, 1},
{ 1, 9 },
{ 8, 2 },
{ 3, 14 }};
double Complex::*var;
auto comparator = [&var](const Complex &a, const Complex &b) { return a.*var < b.*var; };
var = &Complex::real; //Sorting for real numbers
std::sort(vec.begin(), vec.end(), comparator);
var = &Complex::imaginary; //Sorting for imaginary numbers
std::sort(vec.begin(), vec.end(), comparator);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment