ThinkGeo.com    |     Documentation    |     Premium Support

Ordering a collection of features

I’ve been trying to sort/order a collection of features by a certain column value based on the layer selected. Any help/documentation on the this would be greatly appreciated.

Collection closestFeature = dataLayer.FeatureSource.GetFeaturesWithinDistanceOf(position, map.MapUnit, DistanceUnit.Feet, 500, dataLayerColumnNames as IEnumerable);

// code to fill the collection with features. // This works correctly

closestFeature.OrderByDescending(t => t.ColumnValues[“ColumnName”]);

//result does not order the collection

Hi Chad,

As below is the test result, because the collection won’t keep the sort result, so you should want to call ToList to get the correct result.

InMemoryFeatureLayer layer = new InMemoryFeatureLayer();
Feature f1 = new Feature(0, 0);
f1.ColumnValues.Add(“ColumnName”, “2”);

        Feature f2 = new Feature(1, 1);
        f2.ColumnValues.Add("ColumnName", "1");

        Feature f3 = new Feature(2, 2);
        f3.ColumnValues.Add("ColumnName", "3");

        layer.InternalFeatures.Add(f1);
        layer.InternalFeatures.Add(f2);
        layer.InternalFeatures.Add(f3);

        layer.Open();
        Collection<Feature> features = layer.FeatureSource.GetFeaturesWithinDistanceOf(new PointShape(1, 0), GeographyUnit.DecimalDegree, DistanceUnit.Kilometer, 3000, ReturningColumnsType.AllColumns);

        var result1 = features.OrderByDescending(t => t.ColumnValues["ColumnName"]).ToList();
        var result2 = features.OrderByDescending(t => t.Id).ToList();

Or you can use IEnumberable instead of Collection like this:

        IEnumerable<Feature> features = layer.FeatureSource.GetFeaturesWithinDistanceOf(new PointShape(1, 0), GeographyUnit.DecimalDegree, DistanceUnit.Kilometer, 3000, ReturningColumnsType.AllColumns);

        features = features.OrderByDescending(t => t.ColumnValues["ColumnName"]);

Wish that’s helpful.

Regards,

Don