I wanted to know how to alphabetize 2 listboxes together. lstResult contains Names and lstResult2 contains numbers corrisponding to the names in lstResult. When alphabetizing, i want to keep the name in lstResult and number in lstResult2 together.

Dani AI

Generated

— duplicate-thread note already raised by ; suggested a Dictionary mapping names to numbers. That works when names are guaranteed unique, but it silently fails or loses data when duplicates exist. A safer, more general pattern is to pair items by index, sort the pairs, then repopulate both ListBoxes so pairs stay aligned.

A compact, practical example using LINQ (works when each item in lstResult corresponds to the same index in lstResult2):

if (listBox1.Items.Count != listBox2.Items.Count) throw new InvalidOperationException("Mismatched item counts.");

var pairs = listBox1.Items.Cast<object>()
    .Select((item, i) => new { Name = item.ToString().Trim(), Number = listBox2.Items[i].ToString(), Index = i })
    .OrderBy(p => p.Name, StringComparer.CurrentCultureIgnoreCase)
    .ThenBy(p => p.Index)
    .ToList();

listBox1.Items.Clear();
listBox2.Items.Clear();
foreach (var p in pairs) {
    listBox1.Items.Add(p.Name);
    listBox2.Items.Add(p.Number);
}

Notes and troubleshooting:

  • Trimming strings and using a culture-aware, case-insensitive comparer yields predictable alphabetical order.
  • The index field (ThenBy) preserves original relative order for identical names; this avoids ambiguity when duplicates occur.
  • If the controls are data-bound, sort the underlying list or use a BindingSource instead of manipulating Items directly.
  • For a more scalable UI, consider a two-column ListView or a DataGridView so the name/number pair is a single row and column sorting keeps pairs intact.
  • If numeric semantics matter (numbers should sort as integers), parse with int.TryParse before sorting or store the number as an int in the paired object.

This approach keeps pairs intact, handles duplicates, and avoids the unique-key limitation of a Dictionary while remaining straightforward to implement.

Recommended Answers

All 3 Replies

im very sorry about that. it wont happen again.

Happy Newyear to you all!
My first approach (perhaps not the best one) would be:
Put your two listboxes in a Dictionary, something like this:

Dictionary<string, int> d = new Dictionary<string, int>();
        d.Add("cat", 2);
        d.Add("dog", 1);
        d.Add("llama", 0);
        d.Add("iguana", -1);

Now sort your first listbox. Walk through it item by item and fill the second listbox with the number you find after shearching the name in the dictionary.
Alternatively you could sort the dictionary and fill both listboxes.
Succes!

commented: :) Best wishes for a very healthy and happy new year to you and your family. +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.