Click here to Skip to main content
15,885,195 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a bindinglist of keyvaluepairs that is filled dynamicaly.

C#
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) and value(name). I want to sort the first 5 items asc and then the rest items also asc.the sorting must be by value and not by key. example: if I have the values : 4,5,8,7,6,10,9,3,2,1,22 the sorting result must be 4,5,6,7,8 ,1,2,3,9,10,22.Any suggestions?

I use this to split in two lists
C#
List<BindingList<KeyValuePair<int, string>>> listOfLists = new List<BindingList<KeyValuePair<int, string>>>();
                for (int i = 0; i < Homelist.Count(); i += 5)
                {
                    listOfLists.Add(Homelist.Skip(i).Take(5));
                }
but I take the error:
cannot convert from 'System.Collections.Generic.IEnumerable<system.collections.generic.keyvaluepair><int,string>>' to 'System.ComponentModel.BindingList<system.collections.generic.keyvaluepair><int,string>>'
Posted
Updated 11-Apr-14 21:21pm
v3

For sorting the list on value rather than key just provide an appropriate compare method ... have a look at this example http://www.dotnetperls.com/sort-keyvaluepair[^]

If you want to treat the first 5 items differently to the rest then you could split the list into two separate ones before the sorting process ... examples of splitting lists[^]

You would want to join them back together I presume ... http://www.dotnetperls.com/list-concat[^]

[EDIT in response to edit of question]
I'm not overly familiar with linq however when I go to the Definition of Skip or Take then I can see that they return an IEnumerable keyvaluepair. I tried casting, but that produced a run-time exception. Finally came across this ...
XML
static class MyClass
{
    static public BindingList<T> ToBindingList<T>(this IEnumerable<T> source)
    {
        BindingList<T> bindingList = new BindingList<T>();

        foreach (T o in source.ToList())
        {
            bindingList.Add(o);
        }
        return bindingList;
    }
}
From here http://svanbinst.blogspot.co.uk/2010/01/blog-post.html[^]
That will get you over the compile errors and runtime exceptions
 
Share this answer
 
v2
Comments
iratus7 12-Apr-14 2:53am    
I try to split but I have the error that I edit to the question.Any idea?
CHill60 12-Apr-14 5:12am    
I've updated my answer
iratus7 12-Apr-14 5:40am    
Thank you very much.I used your suggestions.
C#
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,>>();
                for (int i = 0; i < 5; i ++)
                {
                    Playinglist.Add(Homelist[i]);
                }
                Playinglist.Sort(Compare);
                
                List<keyvaluepair><int,>> Benchlist = new List<keyvaluepair><int,>>();
                for (int i = 5; i < 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();
                for (int i = 0; i < unionedList.Count(); i++)
                {
                    Homelist.Insert(i, unionedList[i]);
                }
                game.GetHomelist = Homelist;


as CHill60 told
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900