Alphabetize Bitmap Array from Resource C#

117 views Asked by At

I've been trying to alphabetize this Bitmap Array in C# as the resources aren't alphabetized in the resx file.

I've gotten the resource to an array with this code

var resourceSet = Properties.Resources.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentUICulture, true, true);
System.Drawing.Bitmap[] imageArray = resourceSet.OfType<DictionaryEntry>()
    .Select(i => (System.Drawing.Bitmap)i.Value)
    .ToArray();
1

There are 1 answers

2
Jonesopolis On BEST ANSWER

Your problem is that in your Select, you lose the Key that you want to sort on. Use an anonymous type or a new class to keep the name and image:

var imageArray = resourceSet
                     .OfType<DictionaryEntry>()
                     .Select(d => 
                         new
                         {
                            Name = d.Key.ToString(), 
                            Image = (Bitmap)d.Value
                         })
                      .OrderBy(d => d.Name);
                      .ToArray();