Code to sort based on quantities of a data member occuring The setup is that I have a vector of a class, and I need to sort it by the quantity of a data member value, not the value itself. The below code exhibits the correct behavior, and part of that behavior is &quot;stable&quot; sorting where the order of elements should be preserved when possible.
Between the /*** Starting here ***/ and /*** Ending here ***/ comments, are there some Standard Library functions and/or classes that can be substituted? Per the tag, the code needs to exist in C++14. Yes, I realize that the MRE is written with C++17 (maybe some 20, but I compiled it as C++17), but the code in question [hopefully] isn't.
#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;iterator&gt;
#include &lt;random&gt;
#include &lt;utility&gt;
#include &lt;vector&gt;

struct Foo {
  int fizz = 0;
  int buzz = 0;
};

std::ostream &amp;operator&lt;&lt;(std::ostream &amp;sout, const Foo &amp;obj) {
  return sout &lt;&lt; &quot;(&quot; &lt;&lt; obj.fizz &lt;&lt; &quot;, &quot; &lt;&lt; obj.buzz &lt;&lt; &quot;)&quot;;
}

template &lt;typename Container&gt;
void print(const Container &amp;container, std::ostream &amp;sout = std::cout) {
  std::copy(container.begin(), container.end(),
            std::ostream_iterator&lt;typename Container::value_type&gt;(sout, &quot; &quot;));
  sout &lt;&lt; '\n';
}

int main() {
  std::mt19937 prng(std::random_device{}());
  std::uniform_int_distribution&lt;int&gt; dist(0, 9);
  std::uniform_int_distribution&lt;int&gt; large(500, 999);

  std::vector&lt;Foo&gt; devices;
  for (int i = 0; i &lt; 100; ++i) {
    devices.push_back(Foo{.fizz = dist(prng), .buzz = large(prng)});
  }

  print(devices);
  std::cout &lt;&lt; '\n';

  /*** Starting here ***/
  // Counting occurrences
  std::vector&lt;std::pair&lt;int, int&gt;&gt; counts;
  int index = 0;
  for (int i = 0; i &lt; 10; ++index, ++i) {
    counts.emplace_back(index, 0);
  }
  for (auto i : devices) {
    ++counts[i.fizz].second;
  }

  // Descending sort using counts of occurrence of a fizz value
  std::stable_sort(counts.begin(), counts.end(),
                   [](auto a, auto b) { return a.second &gt; b.second; });

  std::vector&lt;int&gt; sort_by(10);
  for (int i = 0; i &lt; 10; ++i) {
    sort_by[i] = counts[i].first;
  }
  print(sort_by);  // Prints fizz values in order of descending count
  std::cout &lt;&lt; '\n';

  // Sort devices according to highest quantity of fizz
  auto walker = devices.begin();
  for (auto i : sort_by) {
    for (auto it = devices.begin(); it != devices.end(); ++it) {
      if (it-&gt;fizz == i) {
        std::iter_swap(walker, it);
        ++walker;
      }
    }
  }
  /*** Ending here ***/

  print(devices);
}
int index = 0;
  for (int i = 0; i &lt; 10; ++index, ++i) {
    counts.emplace_back(index, 0);
  }

Not sure why you use index here which is same as i ?

[](auto a, auto b) { return a.second &gt; b.second; }
for (auto i : sort_by) {

a and b, i can be marked const.

 std::vector&lt;int&gt; sort_by(10);
  for (int i = 0; i &lt; 10; ++i) {
    sort_by[i] = counts[i].first;
  }

Not sure that there's a big improvement, but here's a change anyway.
  std::vector&lt;int&gt; sort_by;
  sort_by.reserve(10);
  std::for_each(counts.begin(), counts.end(), [&amp;sort_by](const auto &amp;i) {
    sort_by.push_back(i.first);
  });


for (auto it = devices.begin(); it != devices.end(); ++it) {
  if (it-&gt;fizz == i) {
    std::iter_swap(walker, it);
    ++walker;
  }
}

Can be made a bit simpler by using range based for loop &amp; address instead of iterator. Note the reference in &amp;it.
for (auto &amp;it : devices) {
  if (it.fizz == i) {
    std::iter_swap(walker, &amp;it);
    ++walker;
  }
}