Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataSet sorting

In DataTable I could sorting with

 dataTable.DefaultView.Sort = "SortField DESC";

I'm getting a DataSet from database, I was wondering could I do a sorting on the DataSet like how I do it in DataTable.

like image 702
Jerry Lam Avatar asked Jun 14 '12 08:06

Jerry Lam


People also ask

What do you mean by data sorting?

Sorting is the process of arranging data into meaningful order so that you can analyze it more effectively. For example, you might want to order sales data by calendar month so that you can produce a graph of sales performance. You can use Discoverer to sort data as follows: sort text data into alphabetical order.

How do you sort a dataset in Python?

Sorting Your DataFrame on a Single Column. To sort the DataFrame based on the values in a single column, you'll use . sort_values() . By default, this will return a new DataFrame sorted in ascending order.

How do I sort a dataset in R?

To sort a data frame in R, use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order.


2 Answers

For advanced sorting needs you might want to use LINQ like described here. Basically it allows creating a DataView from a LINQ query using the System.Data.DataTableExtensions.AsDataFiew extension method.

Alternatively if you are OK with (or maybe even prefer) using an IEnumerable you could use the System.Data.DataTableExtensions.AsEnumerable extension method. For example:

var enumerable = dataSet.Tables[0].AsEnumerable()
                 .OrderBy(x => x.Field<string>("ColumnName")
                 .ThenByDescending(x => x.Field<int?>("OtherColumnName")??0);
like image 199
Zar Shardan Avatar answered Sep 23 '22 21:09

Zar Shardan


you can still access the DataTable from the the data set as follows,

ds.Tables[0].DefaultView.Sort =" criterian";

Hope this helps.

like image 24
Rajesh Subramanian Avatar answered Sep 23 '22 21:09

Rajesh Subramanian