</div><!-- fragment --><h1><aclass="anchor" id="ParallelSortCreate"></a>
Sort a Range of Items</h1>
<p>The task created by <aclass="el" href="classtf_1_1FlowBuilder.html#a7d844e9856c7c65b26ccdb83ffdab1d6" title="constructs a dynamic task to perform STL-styled parallel sort using the std::less<T> comparator,...">tf::Taskflow::sort(B first, E last)</a> sorts the range <code>[first, last)</code> in ascending order using a parallel divide-and-conquer algorithm built on top of introductory sort (introsort). Taskflow recursively splits the range into sub-ranges, sorts each sub-range in parallel across available workers, and merges the sorted sub-ranges back into a fully sorted result. The following diagram illustrates this process on eight elements:</p>
<divclass="dotgraph">
<iframescrolling="no" frameborder="0" src="dot_parallel_sort.svg" width="688" height="500"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>The dashed edges show the recursive splitting phase; the green edges show per-subrange sequential sort at the base case; the orange edges show the parallel merge phase that reconstructs the fully sorted sequence. To correctly use <aclass="el" href="classtf_1_1FlowBuilder.html#a35e180eb63de6c9f28e43185e837a4fa" title="constructs a dynamic task to perform STL-styled parallel sort">tf::Taskflow::sort</a>, the given iterators must be <em>random-accessible</em>. The following example creates a task that sorts a vector in ascending order:</p>
<divclass="ttc" id="aclasstf_1_1Executor_html"><divclass="ttname"><ahref="classtf_1_1Executor.html">tf::Executor</a></div><divclass="ttdoc">class to create an executor</div><divclass="ttdef"><b>Definition</b> executor.hpp:62</div></div>
<divclass="ttc" id="aclasstf_1_1Executor_html_a519777f5783981d534e9e53b99712069"><divclass="ttname"><ahref="classtf_1_1Executor.html#a519777f5783981d534e9e53b99712069">tf::Executor::run</a></div><divclass="ttdeci">tf::Future< void > run(Taskflow &taskflow)</div><divclass="ttdoc">runs a taskflow once</div></div>
<divclass="ttc" id="aclasstf_1_1FlowBuilder_html_a35e180eb63de6c9f28e43185e837a4fa"><divclass="ttname"><ahref="classtf_1_1FlowBuilder.html#a35e180eb63de6c9f28e43185e837a4fa">tf::FlowBuilder::sort</a></div><divclass="ttdeci">Task sort(B first, E last, C cmp)</div><divclass="ttdoc">constructs a dynamic task to perform STL-styled parallel sort</div></div>
<divclass="ttc" id="aclasstf_1_1Task_html"><divclass="ttname"><ahref="classtf_1_1Task.html">tf::Task</a></div><divclass="ttdoc">class to create a task handle over a taskflow node</div><divclass="ttdef"><b>Definition</b> task.hpp:569</div></div>
<divclass="ttc" id="aclasstf_1_1Taskflow_html"><divclass="ttname"><ahref="classtf_1_1Taskflow.html">tf::Taskflow</a></div><divclass="ttdoc">class to create a taskflow object</div><divclass="ttdef"><b>Definition</b> taskflow.hpp:64</div></div>
</div><!-- fragment --><p>By default, elements are compared using the operator "<" during the sorting process.</p>
<dlclass="section note"><dt>Note</dt><dd><aclass="el" href="classtf_1_1FlowBuilder.html#a35e180eb63de6c9f28e43185e837a4fa" title="constructs a dynamic task to perform STL-styled parallel sort">tf::Taskflow::sort</a> is <em>not</em> a stable sorter. Elements that compare equal may appear in any order in the sorted output.</dd></dl>
<p>You can pass iterators by reference using <ahref="https://en.cppreference.com/w/cpp/utility/functional/ref">std::ref</a> to marshal parameter updates between dependent tasks. This is useful when the range is not known at task-graph construction time but is initialized by an upstream task.</p>
<divclass="ttc" id="aclasstf_1_1FlowBuilder_html_a4d52a7fe2814b264846a2085e931652c"><divclass="ttname"><ahref="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">tf::FlowBuilder::emplace</a></div><divclass="ttdeci">Task emplace(C &&callable)</div><divclass="ttdoc">creates a static task</div><divclass="ttdef"><b>Definition</b> flow_builder.hpp:1781</div></div>
<divclass="ttc" id="aclasstf_1_1Task_html_a8c78c453295a553c1c016e4062da8588"><divclass="ttname"><ahref="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">tf::Task::precede</a></div><divclass="ttdeci">Task & precede(Ts &&... tasks)</div><divclass="ttdoc">adds precedence links from this to other tasks</div><divclass="ttdef"><b>Definition</b> task.hpp:1305</div></div>
</div><!-- fragment --><p>When <code>init</code> finishes, <code>first</code> and <code>last</code> point to the initialized data range of <code>data</code>, and the sort task performs parallel sort over all elements.</p>
<p><aclass="el" href="classtf_1_1FlowBuilder.html#a35e180eb63de6c9f28e43185e837a4fa" title="constructs a dynamic task to perform STL-styled parallel sort">tf::Taskflow::sort(B first, E last, C cmp)</a> is an overload of parallel sort that accepts a custom comparator <code>cmp</code>. The following example sorts a vector of integers in <em>descending</em> order:</p>
</div><!-- fragment --><p>A custom comparator is especially useful when sorting a range of objects whose natural ordering is not defined by the operator "<". For example, user-defined structs or classes that must be ordered by a specific field. The following example defines a <code>Student</code> struct and sorts a vector of students by GPA in descending order:</p>