List Dictionary 排序

 

1、List

List<string> temp = new List<string>();
foreach (PlayAgentItem arg in PlayAgentConfigSection.values.Cast<PlayAgentItem>())
{
  temp.Add(arg.IpAddr);
}
temp = temp.OrderBy(u => u).ToList();

 

2、Dictionary

 private Dictionary<IPAddress, Seat> SeatList;
 SeatList.OrderBy(r => r.Value.ID).ToDictionary(r => r.Key, r => r.Value)

Seat类要实现IComparable的CompareTo接口
public class Seat, IComparable<Seat>
{
     /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(Seat obj)
        {
            if (this.ID == obj.ID)
            {
                return 0;
            }
            else if (this.ID > obj.ID)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
}

 

posted @ 2020-05-22 13:56  孤獨龍  阅读(251)  评论(0编辑  收藏  举报