-1

I want to write an comparator/sort function which can give me the values in ascending order. The values are in hex.

Lets say map is of type:

map<string, vector<class_A objects> >

There is one hex property associated with each object of the vector. Lets call that property as 'z'. By hex property, I mean that there is a property which is of type 'hex', in the form of 11'h000 or may be 11'hfff. I want to sort that vector based on the hex property.

What I have currently in my vector is and their 'z' properties are 11'h000, 11'h00b, 11'h007 respectively. I need to sort that vector based upon 'z' property so that it becomes .

I would be happy to write more details in case needed.

Hemant Bhargava
  • 3,251
  • 4
  • 24
  • 45

1 Answers1

1

I presume you're talking about sorting the vector within the map, not the map itself as your question states.

You can use std::sort, with an appropriate lambda to define the comparison:

//suppose myMap is a std::map<string, std::vector<class_A>
std::vector<class_A>& vec = myMap["SomeKey"];
std::sort(vec.begin(), vec.end(), [](const class_A& lhs, const class_A& rhs) { return lhs.z < rhs.z; })
Smeeheey
  • 9,906
  • 23
  • 39