Logo

Programming-Idioms

History of Idiom 28 > diff from v67 to v68

Edit summary for version 68 by TPlant:
New C++ implementation by user [TPlant]

Version 67

2021-04-01, 23:20:03

Version 68

2021-04-01, 23:21:19

Idiom #28 Sort by a property

Sort elements of array-like collection items in ascending order of x.p, where p is a field of the type Item of the objects in items.

Idiom #28 Sort by a property

Sort elements of array-like collection items in ascending order of x.p, where p is a field of the type Item of the objects in items.

Variables
items,x,p
Variables
items,x,p
Imports
#include <algorithm>
Code
struct {
    bool operator()(const Item &lhs, const Item &rhs) const { return lhs.p < rhs.p; }
} custom_sort;

std::sort(begin(items), end(items), custom_sort);
Doc URL
https://en.cppreference.com/w/cpp/algorithm/sort
Demo URL
https://godbolt.org/z/1sb5zWbGM