What would be the efficient way of sorting typeof(EnumType) alphabetically?
The enum values have index non-sequential however sorted in alphabetic order.
(i.e. apple = 5, banana = 2, cantaloupe = 3)
Instantiating temporarily is fine.
Eventually I need the index code for the specific enum value selected.
I am asking because the method I came up with does not look the best:
Array tmp = Enum.GetValues(typeof(EnumType));
string[] myenum = tmp.OfType<object>().Select(o => o.ToString()).ToArray();
Array.Sort(myenum);
int enum_code = (int)Enum.Parse(typeof(EnumType), myenum.GetValue((int)selected_index).ToString());
string final_code = enum_code.ToString());
You could use Linq to write more compact and maintainable code. Unless you’re doing this in the inner loop of a high-performance application, I doubt the speed of Linq vs. your original code vs. any other possible implementation will matter at all: