Sorting selected features

965
4
Jump to solution
02-22-2018 11:42 AM
MassimoMazzanti
New Contributor III

How can I sort a list of selected features based on a field value (i.e. the field name is "cityname")?

My code is this:

...

FeatureQueryResult queryResult = await featureTable.QueryFeaturesAsync(queryParams, QueryFeatureFields.LoadAll);
var features = queryResult.ToList();
// ... here I need to sort the features list

Many thanks

Massimo

0 Kudos
1 Solution

Accepted Solutions
dotMorten_esri
Esri Notable Contributor

You can use LINQ to perform any form of sorting:

var sortedResults = queryResult.OrderBy(f=>f.Attributes["FieldName"])

However you might be better off sorting up front in the query:

var parameters = new Data.QueryParameters();
parameters.OrderByFields.Add(new Data.OrderBy("FieldName", Data.SortOrder.Ascending);

View solution in original post

4 Replies
NagmaYasmin
Occasional Contributor III

Hi Massimo,

You may user features.Sort() method by defining the Comparer, for your case it would be "cityname".

You may take a look at this site: List(T).Sort Method (System.Collections.Generic) 

Hope that helps,

Nagma

MassimoMazzanti
New Contributor III

Thanks for the help. It is correct, but the solution proposed by Morten Nielsen is simpler and more suitable for my needs.

Best regards

Massimo

dotMorten_esri
Esri Notable Contributor

You can use LINQ to perform any form of sorting:

var sortedResults = queryResult.OrderBy(f=>f.Attributes["FieldName"])

However you might be better off sorting up front in the query:

var parameters = new Data.QueryParameters();
parameters.OrderByFields.Add(new Data.OrderBy("FieldName", Data.SortOrder.Ascending);

MassimoMazzanti
New Contributor III

The LINQ solution

var sortedResults = queryResult.OrderBy(f=>f.Attributes["FieldName"])

is perfetct for me.

Many thanks

Massimo

0 Kudos