Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 相对于另一个列表框对列表框进行排序_C#_Sorting_Listbox - Fatal编程技术网

C# 相对于另一个列表框对列表框进行排序

C# 相对于另一个列表框对列表框进行排序,c#,sorting,listbox,C#,Sorting,Listbox,我知道你可以设置一个列表框来自动排序。是否有一种方法可以“捕获”排序,以便当列表框交换两个项目的位置时,我可以在另一个列表框上执行相同的重新排序?我希望按值对一个列表框进行排序,但与其他地方的另一个列表框相比,将这些值保持在相同的相对索引位置 我可以编写一个例程对列表进行气泡排序,这样我就可以自己进行更改,但我想知道是否有更自动化的方法,因为我可能需要在程序中的几个不同位置执行此操作。不幸的是,Sorted属性不使用IComparable接口实现,只是根据项目的ToString结果进行排序。但是

我知道你可以设置一个列表框来自动排序。是否有一种方法可以“捕获”排序,以便当列表框交换两个项目的位置时,我可以在另一个列表框上执行相同的重新排序?我希望按值对一个列表框进行排序,但与其他地方的另一个列表框相比,将这些值保持在相同的相对索引位置


我可以编写一个例程对列表进行气泡排序,这样我就可以自己进行更改,但我想知道是否有更自动化的方法,因为我可能需要在程序中的几个不同位置执行此操作。

不幸的是,
Sorted
属性不使用
IComparable
接口实现,只是根据项目的
ToString
结果进行排序。但是,您可以使用排序数据源(例如,
列表
),而不是设置
Sorted
属性

ListBox
中的项目创建包装类,并在其上实现
IComparable
接口。用这些
ListBoxItem
实例填充
List
,然后调用列表上的
Sort
方法。因此,您将能够将
比较发送到
调用

public partial class Form1 : Form
{
    private class ListBoxItem<T> : IComparable<ListBoxItem<T>>
        where T : IComparable<T>
    {
        private T item;

        internal ListBoxItem(T item)
        {
            this.item = item;
        }

        // this makes possible to cast a string to a ListBoxItem<string>, for example
        public static implicit operator ListBoxItem<T>(T item)
        {
            return new ListBoxItem<T>(item);
        }

        public override string ToString()
        {
            return item.ToString();
        }

        public int CompareTo(ListBoxItem<T> other)
        {                
            return item.CompareTo(other.item); // here you can catch the comparison
        }
    }

    public Form1()
    {
        InitializeComponent();
        var items = new List<ListBoxItem<string>> { "Banana", "Apple"};
        items.Sort();
        listBox1.DataSource = items;
    }
公共部分类表单1:表单
{
私有类ListBoxItem:IComparable
其中T:i可比较
{
私人物品;
内部ListBoxItem(T项)
{
this.item=项目;
}
//例如,这使得可以将字符串强制转换为ListBoxItem
公共静态隐式运算符ListBoxItem(T项)
{
返回新的ListBoxItem(项目);
}
公共重写字符串ToString()
{
return item.ToString();
}
公共整数比较(ListBoxItem其他)
{                
return item.CompareTo(other.item);//在这里可以捕获比较
}
}
公共表格1()
{
初始化组件();
var items=新列表{“香蕉”、“苹果”};
items.Sort();
listBox1.DataSource=项目;
}