C# sort System.InvalidOperationException: Failed to compare two elements in the ar

System.InvalidOperationException: Failed to compare two elements in the array. ---> System.NullReferenceException: Object reference not set to an instance of an object.

第一次用C#sort遇到的,感觉和它的底层实现有关。

抛异常的写法:
List<GuildBattleFieldRank> redList = new List<GuildBattleFieldRank>();
redList.Sort((l, r) =>
            {
                if (l.Score >= r.Score)
                    return -1;
                return 1;
            });

解决方法:
List<GuildBattleFieldRank> redList = new List<GuildBattleFieldRank>();
redList.Sort((l, r) =>
            {
                if (l.Score == r.Score) return 0;
                else if (l.Score > r.Score)
                    return -1;
                return 1;
            });

 

posted on 2019-12-03 11:04  zyz913614263  阅读(1574)  评论(0编辑  收藏  举报

导航