Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.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,我正在反序列化一个保存记录的文本文件,并将记录强制转换为一个产品对象。我将对象添加到列表框中,并按第一个字段排序,该字段是Price,设置为double。我将列表框设置为排序: listBox1.Sorted = true; 但列表框仅按第一位数字排序,即将$15.00置于$3.00之上。按整个价格而不仅仅是第一位数字排序的最佳方式是什么?问题是它不是按数字排序,而是按字母排序, 请设置listBox1.Sorted=false 使用开始按价格对产品列表进行排序,然后将每个元素添加到控件。我想

我正在反序列化一个保存记录的文本文件,并将记录强制转换为一个产品对象。我将对象添加到列表框中,并按第一个字段排序,该字段是Price,设置为double。我将列表框设置为排序:

listBox1.Sorted = true;

但列表框仅按第一位数字排序,即将$15.00置于$3.00之上。按整个价格而不仅仅是第一位数字排序的最佳方式是什么?

问题是它不是按数字排序,而是按字母排序, 请设置
listBox1.Sorted=false

使用开始按价格对产品列表进行排序,然后将每个元素添加到控件。

我想您必须从ListBox派生才能这样做。重写其
void Sort()
方法将实现以下目的:

public class AlphabeticalSortedListBox : ListBox {
    public AlphabeticalSortedListBox() : base() {
        Sorted = true;
    }

    protected override void Sort() {
        // apply your sorting algorithm on this.Items here.
        // You might want to use an algorithm that does well
        // in the best case (e.g. insertion sort [O(n)] to make it easy)
        // because in the common situation we have an almost sorted list of Items
    }
}
可能重复的