Search code examples
c#resx

Alphabetize Bitmap Array from Resource C#


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

Solution

  • 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();