Index: src/Common/utility.h
===================================================================
--- src/Common/utility.h	(revision 5cbacf10c007573bb52e9dbace6bc7d304c98b78)
+++ src/Common/utility.h	(revision 2782f38a71eacbc10ee2e26fb9a636cb17b6ccd4)
@@ -26,4 +26,5 @@
 #include <string>
 #include <type_traits>
+#include <utility>
 
 #include <cassert>
@@ -462,4 +463,38 @@
 std::pair<long long int, bool> eval(Expression * expr);
 
+// -----------------------------------------------------------------------------
+/// Reorders the input range in-place so that the minimal-value elements according to the 
+/// comparator are in front; 
+/// returns the iterator after the last minimal-value element.
+template<typename Iter, typename Compare>
+Iter sort_mins( Iter begin, Iter end, Compare& lt ) {
+	if ( begin == end ) return end;
+	
+	Iter min_pos = begin;
+	for ( Iter i = begin + 1; i != end; ++i ) {
+		if ( lt( *i, *min_pos ) ) {
+			// new minimum cost; swap into first position
+			min_pos = begin;
+			std::iter_swap( min_pos, i );
+		} else if ( ! lt( *min_pos, *i ) ) {
+			// duplicate minimum cost; swap into next minimum position
+			++min_pos;
+			std::iter_swap( min_pos, i );
+		}
+	}
+	return ++min_pos;
+}
+
+template<typename Iter, typename Compare>
+inline Iter sort_mins( Iter begin, Iter end, Compare&& lt ) {
+	return sort_mins( begin, end, lt );
+}
+
+/// sort_mins defaulted to use std::less
+template<typename Iter>
+inline Iter sort_mins( Iter begin, Iter end ) {
+	return sort_mins( begin, end, std::less<typename std::iterator_traits<Iter>::value_type>{} );
+}
+
 // Local Variables: //
 // tab-width: 4 //
