0

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.

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

    }
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Daniel Xi
  • 11
  • 4
  • Maybe a duplicate of http://stackoverflow.com/questions/3667088/sorting-a-list-of-items-in-a-list-box – gorgi93 Apr 08 '13 at 00:11
  • well how does not work. Because this code is written correct – gorgi93 Apr 08 '13 at 00:14
  • you want to sort numbers alphabetically?? – sa_ddam213 Apr 08 '13 at 00:16
  • No, the data elements in the listbox which are basically file names (strings). – Daniel Xi Apr 08 '13 at 00:17
  • So for example, I create a listbox and add the following elements into it: listBox.Items.Add("Hello"); listBox.Items.Add("Steve"); listBox.Items.Add("John"); listBox.Items.Add("John"); – Daniel Xi Apr 08 '13 at 00:18
  • 2
    Why not just set the `ListBox.Sorted` property to `True` and let it do the work for you? – Ken White Apr 08 '13 at 00:28
  • 2
    The `ListBox.Items` collection supports sorting natively. Simply add to the `SortDescriptions` property and you are done: http://msdn.microsoft.com/en-us/library/system.windows.controls.itemcollection.sortdescriptions.aspx – Mike Zboray Apr 08 '13 at 00:30
  • @KenWhite I believe he is using WPF not WinForms but in both frameworks, the controls will sort for you. – Mike Zboray Apr 08 '13 at 00:31

2 Answers2

2

You could try LINQ:

public void sort(int[] array)
{
    array = array.OrderByDescending (a => a).ToArray();
}
devilfish17
  • 337
  • 3
  • 10
0

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.

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

Now, that being said, I would suggest using a List<String> and just skip doing this work by hand altogether. See List.Sort() for reference.

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

    }
}
  • Changing the line `if (array[i] < array[pass])` to `if (array[i].CompareTo(array[pass]) < 0)` in the `ExchangeSort()` method, as well as replacing `int[]` with `String[]` in the appropriate places will solve your problem. Also you will need to change `int temp;` to `String temp;` in the sort method. – Emma Gospodinova Apr 08 '13 at 01:26
  • Thanks for that. It looks like I forgot to replace the int with string. – Daniel Xi Apr 08 '13 at 04:36