Alphabatize all images pulled from a resx file C#

74 views Asked by At

I have a drop down combo box that changes the image of a picture box. The images are stored in a resx file with an array that counts them, so that if I ever decide to add more, I need only to update the combo box, and add the images to the resx file. The problem I'm having is that when I update the image with the combo box, the images in the resx aren't in alphabetical order, but they do change the image on the picture box.

Here is my code

        ResourceSet cardResourceSet = Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);

        attributes = new Image[cardCount];
        cardCount = 0;

        foreach (DictionaryEntry entry in cardResourceSet)
        {
            string resourceKey = (string)entry.Key;
            object resource = entry.Value;

            cardCount++;
        }

        attributes = new Image[cardCount];
        cardCount = 0;

        foreach (DictionaryEntry entry in cardResourceSet)
        {
            attributes[cardCount] = (Image)entry.Value;
            cardCount++;
        }

        if (attributeBox.SelectedIndex != -1)
        {
            this.cardImage.Image = attributes[attributeBox.SelectedIndex];
        }

How can I go about making it sort the resources in the resx alphabetically?

1

There are 1 answers

1
Cᴏʀʏ On BEST ANSWER

The return type of GetResourceSet, ResourceSet, implements IEnumerable, so you should be able to run some LINQ ordering on it:

foreach (var entry in cardResourceSet.Cast<DictionaryEntry>().OrderBy(de => de.Key))
{

}

Since you're iterating over it twice (though I'm not really sure of the point of the first for-loop, unless there's other code), you may want to assign the sorted results to a separate variable:

var sortedCardResourceSet.Cast<DictionaryEntry>().OrderBy(de => de.Key).ToList();

foreach (var entry in sortedCardResourceSet)
{
    ...
}

...