# C1.Win.FlexGrid.C1FlexGridBase.Sort

## Content

<div class="doc-site-dotnet-api-container">



<h1 id="C1_Win_FlexGrid_C1FlexGridBase_Sort_" data-uid="C1.Win.FlexGrid.C1FlexGridBase.Sort*">Sort Method
</h1>
<div class="markdown level0 summary"></div>
<div class="markdown level0 conceptual"></div>



<a id="C1_Win_FlexGrid_C1FlexGridBase_Sort_" data-uid="C1.Win.FlexGrid.C1FlexGridBase.Sort*"></a>
<h4 id="C1_Win_FlexGrid_C1FlexGridBase_Sort_C1_Win_FlexGrid_SortFlags_System_Int32_" data-uid="C1.Win.FlexGrid.C1FlexGridBase.Sort(C1.Win.FlexGrid.SortFlags,System.Int32)">Sort(SortFlags, int)</h4>
<div class="markdown level1 summary"><p>Sorts the grid contents based on a column.</p>
</div>
<div class="markdown level1 conceptual"></div>
<h5 class="declaration">Declaration</h5>
<div class="codewrapper">
  <pre><code class="lang-csharp hljs">public virtual void Sort(SortFlags order, int col)</code></pre>
</div>
<div class="codewrapper">
  <pre><code class="lang-vbnet hljs">Public Overridable Sub Sort(order As SortFlags, col As Integer)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="C1.Win.FlexGrid.SortFlags.html">SortFlags</a></td>
      <td><span class="parametername">order</span></td>
      <td><p><a class="xref" href="C1.Win.FlexGrid.SortFlags.html">SortFlags</a> value that specifies the sort direction and options.</p>
</td>
    </tr>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.int32">int</a></td>
      <td><span class="parametername">col</span></td>
      <td><p>Column to sort on.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 id="C1_Win_FlexGrid_C1FlexGridBase_Sort_C1_Win_FlexGrid_SortFlags_System_Int32__remarks">Remarks</h5>
<div class="markdown level1 remarks"><p>Sorting works differently depending on whether the grid is bound to a data source or not.</p>
<p>In bound mode, the grid passes the sort request to the underlying data source object. In this case, 
the sort settings remain in effect as the grid data changes. Editing values in the sort column will cause 
the data source to re-sort the data, and grid will show the changes automatically. This is equivalent to
setting the <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.data.dataview.sort">Sort</a> property on a <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.data.dataview">DataView</a> object for example.</p>
<p>In unbound mode, the <b>Sort</b> method sorts the data that is currently stored in the grid. Changing
the data after it has been sorted will not automatically update the sort.</p>
<p>When the grid is bound to a new data source, it inherits the sort settings currently applied to the
new data source object. Because of this, calling <b>Sort</b> immediately before setting the <a class="xref" href="C1.Win.FlexGrid.C1FlexGridBase.DataSource.html#C1_Win_FlexGrid_C1FlexGridBase_DataSource">DataSource</a>
property has no effect on the grid.</p>
<p>In unbound mode, the grid uses a stable sorting algorithm. This means that the sorting keeps the 
relative order of records when the sorting key is the same. For example, if you sort a list of files by 
name, then by extension, the list will still be sorted by name within each extension group.</p>
<p>The grid recognizes two types of row: regular rows which contain data, and node rows which are used
to group data. The <b>Sort</b> method only sorts regular rows; it does not affect node rows at all.
To sort nodes, use the <a class="xref" href="C1.Win.FlexGrid.GridTree.Sort.html#C1_Win_FlexGrid_GridTree_Sort_System_Int32_C1_Win_FlexGrid_SortFlags_System_Int32_System_Int32_">Sort(int, SortFlags, int, int)</a> method in the <a class="xref" href="C1.Win.FlexGrid.C1FlexGridBase.Tree.html#C1_Win_FlexGrid_C1FlexGridBase_Tree">Tree</a> property or the
<a class="xref" href="C1.Win.FlexGrid.Node.Sort.html#C1_Win_FlexGrid_Node_Sort_C1_Win_FlexGrid_SortFlags_System_Int32_System_Int32_">Sort(SortFlags, int, int)</a> method in the <a class="xref" href="C1.Win.FlexGrid.Node.html">Node</a> class.</p>
</div>


<a id="C1_Win_FlexGrid_C1FlexGridBase_Sort_" data-uid="C1.Win.FlexGrid.C1FlexGridBase.Sort*"></a>
<h4 id="C1_Win_FlexGrid_C1FlexGridBase_Sort_C1_Win_FlexGrid_SortFlags_System_Int32_System_Int32_" data-uid="C1.Win.FlexGrid.C1FlexGridBase.Sort(C1.Win.FlexGrid.SortFlags,System.Int32,System.Int32)">Sort(SortFlags, int, int)</h4>
<div class="markdown level1 summary"><p>Sorts the grid contents based on a column range.</p>
</div>
<div class="markdown level1 conceptual"></div>
<h5 class="declaration">Declaration</h5>
<div class="codewrapper">
  <pre><code class="lang-csharp hljs">public virtual void Sort(SortFlags order, int col1, int col2)</code></pre>
</div>
<div class="codewrapper">
  <pre><code class="lang-vbnet hljs">Public Overridable Sub Sort(order As SortFlags, col1 As Integer, col2 As Integer)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="C1.Win.FlexGrid.SortFlags.html">SortFlags</a></td>
      <td><span class="parametername">order</span></td>
      <td><p><a class="xref" href="C1.Win.FlexGrid.SortFlags.html">SortFlags</a> value that specifies the sort direction and options.</p>
</td>
    </tr>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.int32">int</a></td>
      <td><span class="parametername">col1</span></td>
      <td><p>First column in the range.</p>
</td>
    </tr>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.int32">int</a></td>
      <td><span class="parametername">col2</span></td>
      <td><p>Last column in the range.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 id="C1_Win_FlexGrid_C1FlexGridBase_Sort_C1_Win_FlexGrid_SortFlags_System_Int32_System_Int32__remarks">Remarks</h5>
<div class="markdown level1 remarks"><p>When you sort multiple columns, the same sorting options are applied to each column, 
starting from the leftmost column in the range and proceeding to the right.</p>
<p>To sort multiple columns using a different sorting order for each, you can either 
call the <a class="xref" href="C1.Win.FlexGrid.C1FlexGridBase.Sort.html#C1_Win_FlexGrid_C1FlexGridBase_Sort_C1_Win_FlexGrid_SortFlags_System_Int32_">Sort(SortFlags, int)</a> method multiple times or set each column's <a class="xref" href="C1.Win.FlexGrid.Column.Sort.html#C1_Win_FlexGrid_Column_Sort">Sort</a> 
property and call the <a class="xref" href="C1.Win.FlexGrid.C1FlexGridBase.Sort.html#C1_Win_FlexGrid_C1FlexGridBase_Sort_C1_Win_FlexGrid_SortFlags_System_Int32_">Sort(SortFlags, int)</a> method including the <a class="xref" href="C1.Win.FlexGrid.SortFlags.html#C1_Win_FlexGrid_SortFlags_UseColSort">UseColSort</a> 
flag in the <code class="paramref">order</code> parameter.</p>
</div>


<a id="C1_Win_FlexGrid_C1FlexGridBase_Sort_" data-uid="C1.Win.FlexGrid.C1FlexGridBase.Sort*"></a>
<h4 id="C1_Win_FlexGrid_C1FlexGridBase_Sort_C1_Win_FlexGrid_SortFlags_C1_Win_FlexGrid_CellRange_" data-uid="C1.Win.FlexGrid.C1FlexGridBase.Sort(C1.Win.FlexGrid.SortFlags,C1.Win.FlexGrid.CellRange)">Sort(SortFlags, CellRange)</h4>
<div class="markdown level1 summary"><p>Sorts a range of cells in the grid.</p>
</div>
<div class="markdown level1 conceptual"></div>
<h5 class="declaration">Declaration</h5>
<div class="codewrapper">
  <pre><code class="lang-csharp hljs">public virtual void Sort(SortFlags order, CellRange rg)</code></pre>
</div>
<div class="codewrapper">
  <pre><code class="lang-vbnet hljs">Public Overridable Sub Sort(order As SortFlags, rg As CellRange)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="C1.Win.FlexGrid.SortFlags.html">SortFlags</a></td>
      <td><span class="parametername">order</span></td>
      <td><p><a class="xref" href="C1.Win.FlexGrid.SortFlags.html">SortFlags</a> value that specifies the sort direction and options.</p>
</td>
    </tr>
    <tr>
      <td><a class="xref" href="C1.Win.FlexGrid.CellRange.html">CellRange</a></td>
      <td><span class="parametername">rg</span></td>
      <td><p><a class="xref" href="C1.Win.FlexGrid.CellRange.html">CellRange</a> object that specifies the range of cells to sort.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 id="C1_Win_FlexGrid_C1FlexGridBase_Sort_C1_Win_FlexGrid_SortFlags_C1_Win_FlexGrid_CellRange__remarks">Remarks</h5>
<div class="markdown level1 remarks"><p>When you sort multiple columns, the same sorting options are applied to each column, 
starting from the leftmost column in the range and proceeding to the right.</p>
<p>To sort multiple columns using a different sorting order for each, you can either 
call the <a class="xref" href="C1.Win.FlexGrid.C1FlexGridBase.Sort.html#C1_Win_FlexGrid_C1FlexGridBase_Sort_C1_Win_FlexGrid_SortFlags_System_Int32_">Sort(SortFlags, int)</a> method multiple times or set each column's <a class="xref" href="C1.Win.FlexGrid.Column.Sort.html#C1_Win_FlexGrid_Column_Sort">Sort</a> 
property and call the <a class="xref" href="C1.Win.FlexGrid.C1FlexGridBase.Sort.html#C1_Win_FlexGrid_C1FlexGridBase_Sort_C1_Win_FlexGrid_SortFlags_System_Int32_">Sort(SortFlags, int)</a> method including the <a class="xref" href="C1.Win.FlexGrid.SortFlags.html#C1_Win_FlexGrid_SortFlags_UseColSort">UseColSort</a> 
flag in the flags parameter.</p>
</div>


<a id="C1_Win_FlexGrid_C1FlexGridBase_Sort_" data-uid="C1.Win.FlexGrid.C1FlexGridBase.Sort*"></a>
<h4 id="C1_Win_FlexGrid_C1FlexGridBase_Sort_System_Int32_System_Int32_System_Collections_IComparer_" data-uid="C1.Win.FlexGrid.C1FlexGridBase.Sort(System.Int32,System.Int32,System.Collections.IComparer)">Sort(int, int, IComparer)</h4>
<div class="markdown level1 summary"><p>Sorts a group of rows using the specified comparer.</p>
</div>
<div class="markdown level1 conceptual"></div>
<h5 class="declaration">Declaration</h5>
<div class="codewrapper">
  <pre><code class="lang-csharp hljs">public virtual void Sort(int rowStart, int rowCount, IComparer comparer)</code></pre>
</div>
<div class="codewrapper">
  <pre><code class="lang-vbnet hljs">Public Overridable Sub Sort(rowStart As Integer, rowCount As Integer, comparer As IComparer)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.int32">int</a></td>
      <td><span class="parametername">rowStart</span></td>
      <td><p>First row in the sort range.</p>
</td>
    </tr>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.int32">int</a></td>
      <td><span class="parametername">rowCount</span></td>
      <td><p>Number of rows in the sort range.</p>
</td>
    </tr>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.icomparer">IComparer</a></td>
      <td><span class="parametername">comparer</span></td>
      <td><p>An <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.icomparer">IComparer</a> object that compares <a class="xref" href="C1.Win.FlexGrid.C1FlexGridBase.Row.html#C1_Win_FlexGrid_C1FlexGridBase_Row">Row</a> objects.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 id="C1_Win_FlexGrid_C1FlexGridBase_Sort_System_Int32_System_Int32_System_Collections_IComparer__remarks">Remarks</h5>
<div class="markdown level1 remarks"><p>The <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.icomparer">IComparer</a> interface has a single method called <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.icomparer.compare">Compare(object, object)</a> that 
takes two objects as arguments (in this case, they will be <a class="xref" href="C1.Win.FlexGrid.C1FlexGridBase.Row.html#C1_Win_FlexGrid_C1FlexGridBase_Row">Row</a> objects) and 
returns -1, 0, or +1. For more details, see the documentation for <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.icomparer">IComparer</a>.</p>
<p>Custom sorting can only be used when the grid is in unbound mode.</p>
</div>


<a id="C1_Win_FlexGrid_C1FlexGridBase_Sort_" data-uid="C1.Win.FlexGrid.C1FlexGridBase.Sort*"></a>
<h4 id="C1_Win_FlexGrid_C1FlexGridBase_Sort_System_Collections_IComparer_" data-uid="C1.Win.FlexGrid.C1FlexGridBase.Sort(System.Collections.IComparer)">Sort(IComparer)</h4>
<div class="markdown level1 summary"><p>Sorts the grid using the specified comparer.</p>
</div>
<div class="markdown level1 conceptual"></div>
<h5 class="declaration">Declaration</h5>
<div class="codewrapper">
  <pre><code class="lang-csharp hljs">public virtual void Sort(IComparer comparer)</code></pre>
</div>
<div class="codewrapper">
  <pre><code class="lang-vbnet hljs">Public Overridable Sub Sort(comparer As IComparer)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.icomparer">IComparer</a></td>
      <td><span class="parametername">comparer</span></td>
      <td><p>An <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.icomparer">IComparer</a> object that compares <a class="xref" href="C1.Win.FlexGrid.C1FlexGridBase.Row.html#C1_Win_FlexGrid_C1FlexGridBase_Row">Row</a> objects.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 id="C1_Win_FlexGrid_C1FlexGridBase_Sort_System_Collections_IComparer__remarks">Remarks</h5>
<div class="markdown level1 remarks"><p>The <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.icomparer">IComparer</a> interface has a single method called <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.icomparer.compare">Compare(object, object)</a> that 
takes two objects as arguments (in this case, they will be <a class="xref" href="C1.Win.FlexGrid.C1FlexGridBase.Row.html#C1_Win_FlexGrid_C1FlexGridBase_Row">Row</a> objects) and 
returns -1, 0, or +1. For more details, see the documentation for <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.collections.icomparer">IComparer</a>.</p>
<p>Custom sorting can only be used when the grid is in unbound mode.</p>
</div>
</div>
