Click here to Skip to main content
15,921,694 members
Home / Discussions / C#
   

C#

 
AnswerRe: Match Two List Box By Sorting ... Pin
Not Active26-Nov-11 8:05
mentorNot Active26-Nov-11 8:05 
AnswerRe: Match Two List Box By Sorting ... Pin
Luc Pattyn26-Nov-11 8:14
sitebuilderLuc Pattyn26-Nov-11 8:14 
GeneralRe: Match Two List Box By Sorting ... Pin
nassimnastaran26-Nov-11 8:42
nassimnastaran26-Nov-11 8:42 
AnswerRe: Match Two List Box By Sorting ... Pin
Luc Pattyn26-Nov-11 9:53
sitebuilderLuc Pattyn26-Nov-11 9:53 
GeneralRe: Match Two List Box By Sorting ... Pin
nassimnastaran26-Nov-11 10:22
nassimnastaran26-Nov-11 10:22 
GeneralRe: Match Two List Box By Sorting ... Pin
Luc Pattyn26-Nov-11 10:34
sitebuilderLuc Pattyn26-Nov-11 10:34 
GeneralRe: Match Two List Box By Sorting ... Pin
nassimnastaran26-Nov-11 10:43
nassimnastaran26-Nov-11 10:43 
AnswerRe: Match Two List Box By Sorting ... Pin
BillWoodruff26-Nov-11 19:55
professionalBillWoodruff26-Nov-11 19:55 
I interpret your question as:

Given two List Boxes:

1. where initially ListBox1 is sorted by some criterion (alpha-numeric ?)

2. where initially each ListBox2 item is associated with the item in ListBox1 with the same index (even though they may "appear" unsorted).

Then:

1. in the case where you sort ListBox2 by some criterion

2. you want the items in ListBox1 to remain in matching ordinal position with their "associated" items in ListBox2.

What's "missing" from the picture ... but we can "guess" at ... is if you always are going to sort ListBox2 the same way: we assume here that you are not sorting by just setting
ListBox2.Sorted = true;
Which will give you an alphabetic ascending sort. Your example result shows a sort of ListBox2 in descending order.

Since ListBox Items are all strings, there is the question of whether you really want to sort the Items in ListBox2 as strings or as numbers.

Solution ?

1. abstract away the data and associations from the ListBoxes:

a. I'd do this with a Dictionary<string, int> : why use string as the Key: because it's unlikely you'd have two identical city-names, but, perhaps, very possible, you'd have identical population ?

2. On Form Load, or whatever, populate this Dictionary with the CityName-Population data.

3. Create a function that populates the ListBoxes based on enumerating the Dictionary<string, int>

4. Create a function that sorts the Dictionary by Value (strings) in descending order: Dictionary + Linq will do this for you.

Example code:
XML
private Dictionary<string, int> PopCityDict;

private void Form1_Load(object sender, EventArgs e)
{
    PopCityDict = new Dictionary<string, int>
    {
        {"City One", 2500},
        {"City Two", 3000},
        {"City Three", 1500}
    };

    InitializeListBoxes();
}

private void InitializeListBoxes()
{
    listBox1.Items.Clear();
    listBox2.Items.Clear();

    foreach (var kvp in PopCityDict)
    {
        listBox1.Items.Add(kvp.Key);
        listBox2.Items.Add(kvp.Value);
    }
}

private void SortDescendingButton_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();
    listBox2.Items.Clear();

    foreach (var kvp in PopCityDict.OrderByDescending(criterion => criterion.Value))
    {
        listBox1.Items.Add(kvp.Key);
        listBox2.Items.Add(kvp.Value);
    }
}
Discussion: The sort will not change the underlying structure (order) of the PopCityDict Dictionary ! Which means to reset the ListBoxes to their original order you can just call
InitializeListBoxes();
If you really want to re-arrange the actual order in the Dictionary: have fun Smile | :)

... edit #1 ...

You could easily implement any number of sorting modes by making a more general-purpose sort routine where the OrderBy#.... Linq method used on 'PopCityDict was chosen based on an input parameter to the sorting routine.

And use of a Dictionary is, I am sure, only one option; you could probably use a List of Tuples, etc.

... end edit #1 ...
Scipio: "That's true, Berganza; and what makes the miracle greater is, that we not only speak, but hold intelligent discourse, as though we had souls capable of reason; whereas we are so far from having it, that the difference between brutes and man consists in this, that man is a rational animal, and the brute is irrational." Cervantes, "Colloquy of Dogs," 1613CE. The two talking dogs, Scipio, Berganza, are hallucinations in the mind of a soldier with plague fever undergoing a "sweating" cure.


modified 27-Nov-11 8:25am.

AnswerRe: Match Two List Box By Sorting ... Pin
DaveyM6926-Nov-11 22:48
professionalDaveyM6926-Nov-11 22:48 
GeneralRe: Match Two List Box By Sorting ... Pin
nassimnastaran27-Nov-11 7:59
nassimnastaran27-Nov-11 7:59 
Questionthread resume with out using sleep method Pin
somasekhara77726-Nov-11 6:11
somasekhara77726-Nov-11 6:11 
AnswerRe: thread resume with out using sleep method Pin
Mark Salsbery26-Nov-11 6:28
Mark Salsbery26-Nov-11 6:28 
AnswerRe: thread resume with out using sleep method Pin
Luc Pattyn26-Nov-11 6:30
sitebuilderLuc Pattyn26-Nov-11 6:30 
QuestionscrollBars Pin
FM726-Nov-11 5:25
FM726-Nov-11 5:25 
AnswerRe: scrollBars Pin
Luc Pattyn26-Nov-11 5:33
sitebuilderLuc Pattyn26-Nov-11 5:33 
GeneralRe: scrollBars Pin
FM726-Nov-11 6:07
FM726-Nov-11 6:07 
GeneralRe: scrollBars Pin
BillWoodruff26-Nov-11 18:52
professionalBillWoodruff26-Nov-11 18:52 
GeneralRe: scrollBars Pin
Luc Pattyn27-Nov-11 1:04
sitebuilderLuc Pattyn27-Nov-11 1:04 
AnswerRe: scrollBars Pin
Jitendra Parida - Jeetu30-Nov-11 19:01
Jitendra Parida - Jeetu30-Nov-11 19:01 
QuestionOriginal datarow value. Pin
paper6726-Nov-11 5:11
paper6726-Nov-11 5:11 
QuestionAutomation InternetExplorer, i am getting crazy! Pin
Laaky25-Nov-11 23:03
Laaky25-Nov-11 23:03 
Questionhelp me: problem with remoting Library Pin
kornkimhour25-Nov-11 21:34
kornkimhour25-Nov-11 21:34 
AnswerRe: help me: problem with remoting Library Pin
Richard MacCutchan25-Nov-11 22:27
mveRichard MacCutchan25-Nov-11 22:27 
GeneralRe: help me: problem with remoting Library Pin
kornkimhour26-Nov-11 3:05
kornkimhour26-Nov-11 3:05 
GeneralRe: help me: problem with remoting Library Pin
Richard MacCutchan26-Nov-11 3:57
mveRichard MacCutchan26-Nov-11 3:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.