List数据集按对象某个属性排序

//按代号进行升序排序(要判断代号是否为空,不然会报错)
                    RowItems1.Sort(delegate(RowData x, RowData y)
                    {
                        if (string.IsNullOrEmpty(x.code) && string.IsNullOrEmpty(y.code))
                        {
                            return 0;
                        }
                        else if (!string.IsNullOrEmpty(x.code) && string.IsNullOrEmpty(y.code))
                            return 1;
                        else if (string.IsNullOrEmpty(x.code) && !string.IsNullOrEmpty(y.code))
                            return -1;
                        else
                            return x.code.CompareTo(y.code);
                    });

其中RowData为类或者结构体,code为属性。

原文地址:https://www.cnblogs.com/charlie-chen2016/p/8036963.html