ObservableCollection 实现添加后排序

按MyClass的Name字段排序 

public class EntityObjectMuster : ObservableCollection<MyClass> 
{
         /// <summary>
        /// 实现排序插入
        /// </summary>
        /// <param name="baseSemObjects"></param>
        public void AttachItem(MyClass entityObject)
        {
            if (this.Count == 0)
            {
                Add(entityObject);
            }
            else
            {
                bool isInsret = false;
                for (int i = 0; i < this.Count; i++)
                {
                    if (String.Compare(this[i].Name, entityObject.Name, false) >= 0)
                    {
                        InsertItem(i, entityObject);
                        isInsret = true;
                        break;
                    }
                }
                if (!isInsret)
                {
                    Add(entityObject);
                }
            }
        }
}


Guess you like

Origin blog.csdn.net/wangyue4/article/details/10002211