json检索后排序数组

时间:2012-10-12 23:38:15

标签: c# json sorting

我有以下代码,它可以解析JSON。

EVENT和SQUAD是JSON数组。

我需要什么代码才能对数组进行排序,例如在TIME上排序EVENT,或在RANK上排序SQUAD?

void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {

            var serializer = new DataContractJsonSerializer(typeof(gameDetails));
            gameDetails jsResult = (gameDetails)serializer.ReadObject(e.Result);

        ...
        }
        [DataContract]
        public class gameDetails
        {
            [DataMember]
            public string HomeTeamScore { get; set; }
            [DataMember]
            public string AwayTeamScore { get; set; }
            [DataMember]
            public string FixtureDate { get; set; }
            [DataMember]
            public string HomeTeamName { get; set; }
            [DataMember]
            public string AwayTeamName { get; set; }
            [DataMember(Name = "EVENT")]
            public EVENT[] EVENTS;
            [DataMember(Name = "SQUADS")]
            public SQUAD[] SQUADS;

        }
        [DataContract]
        public class EVENT
        {
            [DataMember]
            public string TEAMFLAG { get; set; }
            [DataMember]
            public string TIME { get; set; }
            [DataMember]
            public string PLAYER { get; set; }
            [DataMember]
            public string TYPE { get; set; }
        }

        [DataContract]
        public class SQUAD
        {
            [DataMember]
            public string PLSURN { get; set; }
            [DataMember]
            public string PLID { get; set; }
            [DataMember]
            public string PLAYER { get; set; }
            [DataMember]
            public string TEAM { get; set; }
            [DataMember]
            public string PLFORN { get; set; }
            [DataMember]
            public string SQUADNO { get; set; }
            [DataMember]
            public string RANK { get; set; }
        }

1 个答案:

答案 0 :(得分:1)

也许这可以用于排序事件:

jsResult.EVENTS = jsResult.EVENTS.OrderBy(e=>e.TIME).ToArray();

请注意,TIME是字符串,您可能希望在排序之前将其转换。