Data table sorting method in place
In many cases, a data table needs to be sorted based on specific columns. For example, a data table with two columns (COL1 and COL2) needs to be sorted by the value of the COL2 column in descending order.
You may first think of using the DefaultView object:
<code>ft.DefaultView.Sort = "COL2 desc"; ft = ft.DefaultView.ToTable(true);</code>
However, this method only sorts the DataView and does not change the DataTable itself. To sort the DataTable directly, a different approach is required.
Use DataView for sorting
While you cannot sort a DataTable directly, you can create a sorted version of it using a DataView:
<code>DataView dv = ft.DefaultView; dv.Sort = "occr desc"; DataTable sortedDT = dv.ToTable();</code>
This method creates a new DataTable (sortedDT) whose contents are sorted according to the specified criteria. The original DataTable (ft) remains unchanged.
The above is the detailed content of How Can I Efficiently Sort a DataTable In-Place?. For more information, please follow other related articles on the PHP Chinese website!