c# - SortedList doesn't seem to be sorting -


i made wrapper class around sortedlist. add objects class expecting them automatically sorted alphabetically, when bind listbox in wpf, see in unsorted order.

public class sortedobservablecollection<t> : icollection<t>, inotifypropertychanged, inotifycollectionchanged t : inotifypropertychanged//, icomparable<t> {     private readonly sortedlist<string,t> _innersortedlist;      public sortedobservablecollection()     {         _innersortedlist = new sortedlist<string, t>();     }      public void add(t item)     {         _innersortedlist.add(item.tostring(), item);         this.onpropertychanged("count");         this.onpropertychanged("item[]");         this.oncollectionchanged(notifycollectionchangedaction.add, item);         item.propertychanged += itempropertychanged;     }      void itempropertychanged(object sender, propertychangedeventargs e)     {         throw new notimplementedexception();     }      public void clear()     {         _innersortedlist.clear();     }      public bool contains(t item)     {         return _innersortedlist.containskey(item.tostring());     }      public void copyto(t[] array, int arrayindex)     {         throw new notimplementedexception();     }      public int count     {         { return _innersortedlist.count; }     }      public bool isreadonly     {         { throw new notimplementedexception(); }     }      public bool remove(t item)     {         bool success = _innersortedlist.remove(item.tostring());         if (success)         {             item.propertychanged -= itempropertychanged;             this.onpropertychanged("count");             this.onpropertychanged("item[]");             this.oncollectionchanged(notifycollectionchangedaction.remove, item);         }         return success;     }      public ienumerator<t> getenumerator()     {         throw new notimplementedexception();     }      system.collections.ienumerator system.collections.ienumerable.getenumerator()     {         return _innersortedlist.getenumerator();     }      protected virtual void onpropertychanged([callermembername] string propertyname = "")     {         if (this.propertychanged != null)         {             this.propertychanged(this, new propertychangedeventargs(propertyname));         }     }      protected virtual void oncollectionchanged(notifycollectionchangedaction action, object item)     {         if (this.collectionchanged != null)         {             this.collectionchanged(this, new notifycollectionchangedeventargs(action, item));         }     }      public event propertychangedeventhandler propertychanged;     public event notifycollectionchangedeventhandler collectionchanged; } 

to bind :

sortedobservablecollection<ircuser> users { get; private set; } .. fill users... lstusers.itemssource = users; 

sample input :

5muhammad0 2muhammad1 5muhammad2 

the output shows similar, ones beginning 2, 4 etc riddled between 5's.

note: ircuser class did implement icomparable, since want alphabetical sort commented implentation out hoping default sorting kick in.

note 2: have override tostring() method, forgot mention :

public override string tostring() {     return (int)type + nick; } 

update :

it seems internally sortedlist maintains right order, not passed listbox in right order...

you not correctly create event object notifycollectionchangedeventargs. object has different overloads of constructor depending on action. must use overload uses index of new item when create new item:

new notifycollectionchangedeventargs(action, item, index) 

here's quote msdn:

initializes new instance of notifycollectionchangedeventargs class describes add or remove change.

notifycollectionchangedeventargs(notifycollectionchangedaction, object, int32) 

update 0

also better not use overload of method tostring compare items, , use special icomparer<tkey> this. in case, correct code looks this:

public void add(t item) {     var key = item.tostring();     _innersortedlist.add(key, item);     this.onpropertychanged("count");     this.onpropertychanged("item[]");     this.oncollectionchanged(new notifycollectionchangedeventargs(notifycollectionchangedaction.add, item, _innersortedlist.indexofkey(key)));     item.propertychanged += itempropertychanged; }  public bool remove(t item) {     var indexofkey = _innersortedlist.indexofkey(item.tostring());     if (indexofkey == -1)         return false;     _innersortedlist.removeat(indexofkey);     item.propertychanged -= itempropertychanged;     this.onpropertychanged("count");     this.onpropertychanged("item[]");     this.oncollectionchanged(new notifycollectionchangedeventargs(notifycollectionchangedaction.remove, item,         indexofkey));     return true; }  public ienumerator<t> getenumerator() {     return _innersortedlist.values.getenumerator(); }  system.collections.ienumerator system.collections.ienumerable.getenumerator() {     return getenumerator(); }  protected virtual void oncollectionchanged(notifycollectionchangedeventargs args) {     var handler = this.collectionchanged;     if (handler != null)     {         handler(this, args);     } } 

Comments