Problem with inbuilt sort() function in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • noob15
    New Member
    • Jun 2010
    • 13

    Problem with inbuilt sort() function in c++

    Explaining doubt with example ::

    I have a vector of pair say vv having values vv[0]=(9,1) vv[1]=(13,2) vv[2]=(9,3) vv[3]=(6,4) vv[4]=(9,5)

    and an array 'aa' having values {5,10,1,4,3}

    now using sort func., i want to sort my vector of pair such that if vv[i].first==vv[j].first then the one having larger value of aa[vv[i].second] and aa[vv[j].second] should come first otherwise normal sort.

    so in this case for vector sorts to (6,4) (9,1) (9,5) (9,3) (13,2) because aa[1]>aa[5]>aa[3]

    How should i modify inbuilt sort() to implement such behaviour.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Since vector does not have a sort method i assume by built in sort you are referring to std::sort from algorithm.

    There is no need to modify the sort itself, just use the second overload that accepts a comparison function or functor as the third parameter and write a function or functor to implement your desired comparison using both vectors.

    Comment

    Working...