c# - custom sorting in a bindinglist of keyvaluepairs -
i have bindinglist of keyvaluepair filled dynamicaly.
bindinglist<keyvaluepair<int, string>> homelist = new bindinglist<keyvaluepair<int, string>>(); foreach (listitem item in listbox2.items) { homelist.add(new keyvaluepair<int, string>(item.id, item.name)); }
the list has key(id) , value(text) shown
i want sort first 5 items asc , rest items asc.the sorting must value , not key. example: if have values : 4,5,8,7,6,10,9,3,2,1,22 sorting result must 4,5,6,7,8 ,1,2,3,9,10,22.any idea?
solved answer:
public int compare(keyvaluepair<int,string> a, keyvaluepair<int,string> b) { return a.value.compareto(b.value); } list<keyvaluepair><int,>> playinglist = new list<keyvaluepair><int,>>(); (int = 0; < 5; ++) { playinglist.add(homelist[i]); } playinglist.sort(compare); list<keyvaluepair><int,>> benchlist = new list<keyvaluepair><int,>>(); (int = 5; < homelist.count(); i++) { benchlist.add(homelist[i]); } benchlist.sort(compare); //union 2 lists var unionedlist = new list<keyvaluepair><int,>>(); unionedlist.addrange(playinglist.union(benchlist)); homelist.clear(); (int = 0; < unionedlist.count(); i++) { homelist.insert(i, unionedlist[i]); } game.gethomelist = homelist;
the idea tony hopkinson said chop list two. sort them separately , join them togther.after clear bindlist , filled join list. sorting can applied in list<> , not bindlists<>.the answere in edited question
Comments
Post a Comment