2 Replies - 190 Views - Last Post: 08 February 2013 - 03:27 PM Rate Topic: -----

#1 krillov  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 18-November 12

Question about vector sorting / operator overloading

Posted 08 February 2013 - 03:01 PM

Hey!

How to start .. I have a vector of objects. Id like to sort the vector by different things like name, etc. If I understood right, using sort algorithm like this only
sort(persons.begin(), persons.end())
would basically use my overloaded " operator< ". My question is how can I tell the sort algorithm that it would sort the vector by name or by other part of the object? I found this site on the net: http://msdn.microsof...x(v=vs.80).aspx
As I see the parameters of the operator function are only the objects of the vector and its only written in the function by which part of object it should sort the vector. I could write more operator functions but how would the algorithm know which one to call? Its kind of hard for me to get familiar with the object oriented part so I appreciate any help. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Question about vector sorting / operator overloading

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4062
  • View blog
  • Posts: 12,541
  • Joined: 25-December 09

Re: Question about vector sorting / operator overloading

Posted 08 February 2013 - 03:06 PM

There is another version of std::sort where you provide the sort comparison function you may want to investigate that version. Here is another example: std::sort

Jim

This post has been edited by jimblumberg: 08 February 2013 - 03:08 PM

Was This Post Helpful? 1
  • +
  • -

#3 krillov  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 18-November 12

Re: Question about vector sorting / operator overloading

Posted 08 February 2013 - 03:27 PM

Thanks! I will take a look. Must find out something. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1