天天看點

C#中List<T>排序

首先,List中T的定義如下:

/// <summary>
    /// GeoJSON格式的參數
    /// </summary>
    public class BodyParamShapeDistance
    {
        /// <summary>
        /// 幾何對象
        /// </summary>
        [Newtonsoft.Json.JsonProperty("Shape")]
        public GeoJSON.Net.Geometry.IGeometryObject Shape { get; set; }

        /// <summary>
        /// 距離
        /// </summary>
        [Newtonsoft.Json.JsonProperty("Distance")]
        public double Distance { get; set; }
    }
           

我們定義一個List<T>:

List<BodyParamShapeDistance> lsResult = new List<BodyParamShapeDistance>() { };
           

并對進進行指派(代碼省略)。

然後,按照其Distance進行排序:

lsResult.OrderBy((e1) => { return System.Convert.ToDouble(e1.Distance); }).ToList()
           

結果如下:

[
    {
        "Shape": {
            "type": "Point",
            "coordinates": [
                13733091.6109,
                5126223.190200001
            ]
        },
        "Distance": 0.6398240774310442
    },
    {
        "Shape": {
            "type": "Point",
            "coordinates": [
                13733110.031999998,
                5126207.581500001
            ]
        },
        "Distance": 24.49381893718201
    },
    {
        "Shape": {
            "type": "Point",
            "coordinates": [
                13733116.322999999,
                5126215.005900003
            ]
        },
        "Distance": 26.55484821473176
    },
    {
        "Shape": {
            "type": "Point",
            "coordinates": [
                13733116.5524,
                5126215.2766999975
            ]
        },
        "Distance": 26.69409126958543
    },
    {
        "Shape": {
            "type": "Point",
            "coordinates": [
                13733117.426799998,
                5126216.308700003
            ]
        },
        "Distance": 27.260763998304128
    },
    {
        "Shape": {
            "type": "Point",
            "coordinates": [
                13733111.483900003,
                5126241.340800002
            ]
        },
        "Distance": 27.495001437711842
    },
    {
        "Shape": {
            "type": "Point",
            "coordinates": [
                13733089.886,
                5126250.759099998
            ]
        },
        "Distance": 27.78144396311649
    },
    {
        "Shape": {
            "type": "Point",
            "coordinates": [
                13733118.289399996,
                5126217.326700002
            ]
        },
        "Distance": 27.872884404584443
    }
]