Sort items in a duplicate message box in Google protocol buffers

Is there an implementation in the protocol buffer library that allows you to sort an array that is listed as a repeating field? For example, for example, an array consists of elements of a type that itself contains an index field, based on which the elements of the array must be sorted. I could not find it, so guess that I would have to write it myself. Just wanted to confirm. Thanks.

+8
c ++ protocol-buffers
source share
1 answer

Protobufs provide the RepeatedPtr interface with mutable_ * methods, which can be sorted using the std :: sort () pattern.

If the basic type of a repeating field is simple, you will most likely want to use the overloaded <, compator, or lambda operator for this. Example toy using lambda:

message StaffMember { optional string name = 1; optional double hourly_rate = 2; } message StoreData { repeated StaffMember staff = 1; } StoreData store; // Reorder the list of staff by pay scale std::sort(store->mutable_staff()->begin(), store->mutable_staff()->end(), [](const StaffMember& a, const StaffMember& b){ return a.hourly_rate() < b.hourly_rate(); }); 
+10
source share

All Articles