<para>Taskflow provides template functions for constructing tasks to sort ranges of items in parallel.</para>
<sect1id="ParallelSort_1ParallelSortInclude">
<title>Include the Header</title><para>You need to include the header file, <computeroutput>taskflow/algorithm/sort.hpp</computeroutput>, for creating a parallel-sort task.</para>
<title>Sort a Range of Items</title><para>The task created by <refrefid="classtf_1_1FlowBuilder_1a7d844e9856c7c65b26ccdb83ffdab1d6"kindref="member">tf::Taskflow::sort(B first, E last)</ref> performs parallel sort to rank a range of elements specified by <computeroutput>[first, last)</computeroutput> in increasing order. The given iterators must be <emphasis>random-accessible</emphasis>. The following example creates a task to sort a data vector in increasing order.</para>
<title>Sort a Range of Items with a Custom Comparator</title><para><refrefid="classtf_1_1FlowBuilder_1a35e180eb63de6c9f28e43185e837a4fa"kindref="member">tf::Taskflow::sort(B first, E last, C cmp)</ref> is an overload of parallel sort that allows users to specify a custom comparator. The following example sorts a data vector in decreasing order.</para>
<para><simplesectkind="attention"><para><refrefid="classtf_1_1FlowBuilder_1a35e180eb63de6c9f28e43185e837a4fa"kindref="member">tf::Taskflow::sort</ref> is not stable. That is, two or more objects with equal keys may not appear in the same order before sorting.</para>
<title>Enable Stateful Data Passing</title><para>The iterators taken by <refrefid="classtf_1_1FlowBuilder_1a35e180eb63de6c9f28e43185e837a4fa"kindref="member">tf::Taskflow::sort</ref> are templated. You can use <refrefid="cpp/utility/functional/reference_wrapper"kindref="compound"external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::reference_wrapper</ref> to enable stateful data passing between the sort task and others. The following example creates a task <computeroutput>init</computeroutput> to initialize the data vector and a task <computeroutput>sort</computeroutput> to sort the data in parallel after <computeroutput>init</computeroutput> finishes.</para>