简体   繁体   English

修改排序算法以使用列表框?

[英]Modify sorting algorithm to work with listboxes?

So I'm trying to figure out to modify this integer sorting algorithm to work with data elements (file names) alphabetically in a listbox but have no idea how? 所以我试图找出修改这个整数排序算法,以便在列表框中按字母顺序处理数据元素(文件名),但不知道如何?

I understand how the sorting algorithm below works and can implement it using an integer array. 我理解下面的排序算法是如何工作的,并且可以使用整数数组来实现它。 However, for listBoxes I can't seem to find any relevant examples on the net. 但是,对于listBoxes,我似乎无法在网上找到任何相关的例子。

public partial class MainWindow : Window
{

    Random rand = new Random();
    int numOfIntegers = 1000;
    int[] array;

    public MainWindow()
    {

        InitializeComponent();

        array = new int[numOfIntegers]; 

    }


    // sort a vector of type int using exchange sort
    public void ExchangeSort(int[] array)
    {
        int pass, i, n = array.Length;
        int temp;
        // make n-1 passes through the data 
        for (pass = 0; pass < n - 1; pass++)
        {
            // locate least of array[pass] ... array[n - 1]  
            // at array[pass] 
            for (i = pass + 1; i < n; i++)
            {
                if (array[i] < array[pass])
                {
                    temp = array[pass];
                    array[pass] = array[i];
                    array[i] = temp;
                }
            }
        }
    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        ExchangeSort(array);
        listBox.Items.Clear();
        foreach (int i in array)
        {
            listBox.Items.Add(i);
        }
        MessageBox.Show("Done");

    }

You could try LINQ: 您可以尝试LINQ:

public void sort(int[] array)
{
    array = array.OrderByDescending (a => a).ToArray();
}

If I understand correctly, you're trying to sort strings. 如果我理解正确,你正在尝试排序字符串。 To compare strings you could simply use the String.CompareTo() method or if you need more than simple comparison, the StringComparator class should do for most use cases. 要比较字符串,您只需使用String.CompareTo()方法,或者如果您需要的不仅仅是简单的比较, StringComparator应该适用于大多数用例。

If you choose to do it this way, the condition while sorting would be something like this: 如果你选择这样做,排序时的条件将是这样的:

if (array[i].CompareTo(array[pass]) < 0)

And the rest of the code would probably stay about the same, except of course changing the int[] to String[] . 其余的代码可能会保持不变,除了当然将int[]更改为String[]

Now, that being said, I would suggest using a List<String> and just skip doing this work by hand altogether. 现在,话虽如此,我建议使用List<String>然后完全跳过这个工作。 See List.Sort() for reference. 请参阅List.Sort()以供参考。

To be a bit more specific, here's an example based on your code of what I mean. 更具体一点,这是一个基于你的代码的例子。

public partial class MainWindow : Window
{
    List<String> items;

    public MainWindow()
    {

        InitializeComponent();
        items = new List<String>(); 
        // Fill your list with whatever items you need
    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        items.Sort();
        listBox.Items.Clear();
        foreach (String str in items)
        {
            listBox.Items.Add(str);
        }
        MessageBox.Show("Done");

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM