<li><ahref="#ParallelSortInclude">Include the Header</a></li>
<li><ahref="#SortARangeOfItems">Sort a Range of Items</a></li>
<li><ahref="#SortARangeOfItemsWithACustomComparator">Sort a Range of Items with a Custom Comparator</a></li>
<li><ahref="#ParallelSortEnableStatefulDataPassing">Enable Stateful Data Passing</a></li>
</ul>
</nav>
<p>Taskflow provides template functions for constructing tasks to sort ranges of items in parallel.</p><sectionid="ParallelSortInclude"><h2><ahref="#ParallelSortInclude">Include the Header</a></h2><p>You need to include the header file, <code>taskflow/algorithm/sort.hpp</code>, for creating a parallel-sort task.</p><preclass="m-code"><spanclass="cp">#include</span><spanclass="w"></span><spanclass="cpf"><taskflow/algorithm/sort.hpp></span><spanclass="cp"></span></pre></section><sectionid="SortARangeOfItems"><h2><ahref="#SortARangeOfItems">Sort a Range of Items</a></h2><p>The task created by <ahref="classtf_1_1FlowBuilder.html#a7d844e9856c7c65b26ccdb83ffdab1d6" class="m-doc">tf::<wbr/>Taskflow::<wbr/>sort(B first, E last)</a> performs parallel sort to rank a range of elements specified by <code>[first, last)</code> in increasing order. The given iterators must be <em>random-accessible</em>. The following example creates a task to sort a data vector in increasing order.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">;</span><spanclass="w"></span>
<spanclass="n">assert</span><spanclass="p">(</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">is_sorted</span><spanclass="p">(</span><spanclass="n">data</span><spanclass="p">.</span><spanclass="n">begin</span><spanclass="p">(),</span><spanclass="w"></span><spanclass="n">data</span><spanclass="p">.</span><spanclass="n">end</span><spanclass="p">()));</span><spanclass="w"></span></pre><asideclass="m-note m-info"><h4>Note</h4><p>Elements are compared using the operator <code><</code>.</p></aside></section><sectionid="SortARangeOfItemsWithACustomComparator"><h2><ahref="#SortARangeOfItemsWithACustomComparator">Sort a Range of Items with a Custom Comparator</a></h2><p><ahref="classtf_1_1FlowBuilder.html#a35e180eb63de6c9f28e43185e837a4fa" class="m-doc">tf::<wbr/>Taskflow::<wbr/>sort(B first, E last, C cmp)</a> is an overload of parallel sort that allows users to specify a custom comparator. The following example sorts a data vector in decreasing order.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">;</span><spanclass="w"></span>
<spanclass="n">assert</span><spanclass="p">(</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">is_sorted</span><spanclass="p">(</span><spanclass="n">data</span><spanclass="p">.</span><spanclass="n">begin</span><spanclass="p">(),</span><spanclass="w"></span><spanclass="n">data</span><spanclass="p">.</span><spanclass="n">end</span><spanclass="p">(),</span><spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">greater</span><spanclass="o"><</span><spanclass="kt">int</span><spanclass="o">></span><spanclass="p">{}));</span><spanclass="w"></span></pre><asideclass="m-note m-info"><h4>Note</h4><p><ahref="classtf_1_1FlowBuilder.html#a35e180eb63de6c9f28e43185e837a4fa" class="m-doc">tf::<wbr/>Taskflow::<wbr/>sort</a> is not stable. That is, two or more objects with equal keys may not appear in the same order before sorting.</p></aside></section><sectionid="ParallelSortEnableStatefulDataPassing"><h2><ahref="#ParallelSortEnableStatefulDataPassing">Enable Stateful Data Passing</a></h2><p>The iterators taken by <ahref="classtf_1_1FlowBuilder.html#a35e180eb63de6c9f28e43185e837a4fa" class="m-doc">tf::<wbr/>Taskflow::<wbr/>sort</a> are templated. You can use <ahref="http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper.html" class="m-doc-external">std::<wbr/>reference_wrapper</a> to enable stateful data passing between the sort task and others. The following example creates a task <code>init</code> to initialize the data vector and a task <code>sort</code> to sort the data in parallel after <code>init</code> finishes.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">;</span><spanclass="w"></span>