C# Winforms在列表框中显示字典中的信息(按值排序)

C# Winforms在列表框中显示字典中的信息(按值排序),c#,winforms,dictionary,listbox,C#,Winforms,Dictionary,Listbox,所以,我有一个排序字典,其中键是名字,值是姓氏 我的表单上有一个列表框,显示所有可能的值,用户可以从中选择姓氏。但是,由于名字是键,因此列表框中显示的这些值不会排序 Andrews -> Carter -> Johnson Carter -> Johnson -> Andrews 我希望姓氏按字母顺序排列在第一行,而不是现在(第二行) 创建一个只包含值的列表是最好的方法吗?我找不到任何方法对列表框的内容进行内部排序,因此我不知道还能做些什么。您可以按照正确的顺序创建另

所以,我有一个排序字典,其中键是名字,值是姓氏

我的表单上有一个列表框,显示所有可能的值,用户可以从中选择姓氏。但是,由于名字是键,因此列表框中显示的这些值不会排序

Andrews -> Carter -> Johnson

Carter -> Johnson -> Andrews
我希望姓氏按字母顺序排列在第一行,而不是现在(第二行)


创建一个只包含值的列表是最好的方法吗?我找不到任何方法对列表框的内容进行内部排序,因此我不知道还能做些什么。

您可以按照正确的顺序创建另一个列表,方法如下:

// Given that 'Person' for the sake of this example is:

public class Person
{
    public string FirstName;
    public string LastName;
}

// And you have a dictionary sorted by Person.FirstName:

var dict = new SortedDictionary<string, Person>();
// ...initialise dict...

// Make list reference a List<Person> ordered by the person's LastName

var list = (from entry in dict
            orderby entry.Value.LastName
            select entry.Value).ToList();

// Use list to populate the listbox
//在本例中,给定的“人”是:
公共阶层人士
{
公共字符串名;
公共字符串LastName;
}
//你有一本按人名排序的字典。名字:
var dict=new SortedDictionary();
//…初始化命令。。。
//使列表引用按此人姓氏排序的列表
变量列表=(来自dict中的条目
orderby entry.Value.LastName
选择entry.Value).ToList();
//使用列表填充列表框
这样做的好处是不修改原始集合(如果这对您很重要的话)

在两个不同的集合中保留对对象的引用的开销不是很高,因为每个对象只有一个引用(加上
列表
实现本身的一些固定开销)

但请记住,只要其中一个集合处于活动状态,对象就会保持活动状态(除非明确清除集合)


可编译样本:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    public static class Program
    {
        private static void Main()
        {
            dict = new SortedDictionary<string, Person>();

            addPerson("iain", "banks");
            addPerson("peter", "hamilton");
            addPerson("douglas", "adams");
            addPerson("arthur", "clark");
            addPerson("isaac", "asimov");

            Console.WriteLine("--------------");

            foreach (var person in dict)
                Console.WriteLine(person.Value);

            var list = (from entry in dict
                        orderby entry.Value.LastName
                        select entry.Value).ToList();

            Console.WriteLine("--------------");

            foreach (var person in list)
                Console.WriteLine(person);
        }

        private static void addPerson(string firstname, string lastname)
        {
            dict.Add(firstname, new Person{FirstName = firstname, LastName = lastname});
        }

        private static SortedDictionary<string, Person> dict;
    }

    public class Person
    {
        public string FirstName;
        public string LastName;
        public override string ToString(){return FirstName + " " + LastName;}
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间演示
{
公共静态类程序
{
私有静态void Main()
{
dict=新的SortedDictionary();
addPerson(“iain”、“银行”);
addPerson(“彼得”、“汉密尔顿”);
addPerson(“道格拉斯”、“亚当斯”);
addPerson(“亚瑟”、“克拉克”);
addPerson(“艾萨克”、“阿西莫夫”);
Console.WriteLine(“--------------”;
foreach(dict中的var人员)
Console.WriteLine(person.Value);
变量列表=(来自dict中的条目
orderby entry.Value.LastName
选择entry.Value).ToList();
Console.WriteLine(“--------------”;
foreach(列表中的var人员)
控制台。写线(人);
}
私有静态void addPerson(字符串firstname、字符串lastname)
{
dict.Add(firstname,新人{firstname=firstname,LastName=LastName});
}
专用静态分类词典;
}
公共阶层人士
{
公共字符串名;
公共字符串LastName;
公共重写字符串ToString(){return FirstName+“”+LastName;}
}
}

您只需在填写列表框之前对字典进行排序即可。您必须使用LINQ来实现这一点:
myDict.OrderBy(x=>x.Key)查看此问题