在keyvaluepairs的绑定列表中对特定项进行排序 [英] Sort specific items in a bindinglist of keyvaluepairs

查看:80
本文介绍了在keyvaluepairs的绑定列表中对特定项进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态填充的keyvaluepairs绑定列表。



I have a bindinglist of keyvaluepairs that is 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));
    }





该列表包含密钥(id)和值(名称)。我想排序前5个项目asc然后其余项目也是asc.the排序必须是按值而不是按键。例如:如果我有值:4,5,8,7,6,10,9,3,2,1,22,排序结果必须是4,5,6,7,8,1,2,3, 9,10,22。任何建议?



我用这个分成两个列表



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

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));
                }

但是我接受了错误:

无法从'System.Collections.Generic.IEnumerable< system.collections.generic.keyvaluepair>< int转换string>>'to'System.ComponentModel.BindingList< system.collections.generic.keyvaluepair>< int,string>>'

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>>'

推荐答案

用于排序列出值而不是键只提供一个合适的比较方法...看看这个例子 http://www.dotnetperls.com / sort-keyvaluepair [ ^ ]



如果你想对前5个项目区别对待,那么你可以在排序过程之前将列表分成两个独立的项目...... 拆分列表的示例 [ ^ ]



您可能希望将它们重新组合在一起我假设... http://www.dotnetperls.com/list-concat [ ^ ]



[为编辑问题而编辑]

我对linq并不过分熟悉,但是当我去Skip的定义或Take时我可以看到他们返回一个IEnumerable keyvaluepair。我尝试了转换,但这产生了一个运行时异常。终于遇到了这个......

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[^]


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 ...
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;
    }
}

从这里 http:/ /svanbinst.blogspot.co.uk/2010/01/blog-post.html [ ^ ]

这将帮助您解决编译错误和运行时异常

From here http://svanbinst.blogspot.co.uk/2010/01/blog-post.html[^]
That will get you over the compile errors and runtime exceptions


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;





如CHill60告诉



as CHill60 told


这篇关于在keyvaluepairs的绑定列表中对特定项进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆