'how do you sort time zones please?

Hi I'm using the following to populate a drop down list. I would prefer the list was sorted alphabetically on ID rather than the default.

ReadOnlyCollection zones = TimeZoneInfo.GetSystemTimeZones().OrderBy(??);

Im struggling with the Orderby syntax - what needs to go in here please?



Solution 1:[1]

From your comment:

I was hoping to sort by ID to make it easier for web user to find their zone.

If you really wanted to do that, it would be like this:

var zones = TimeZoneInfo.GetSystemTimeZones().OrderBy(zone => zone.Id);

However, I strongly suggest that you don't do that. The list returned by GetSystemTimeZones is already sorted in an order that is convenient for the user to find their zone, as long as you are presenting the list properly.

If you are showing them the Id property, then that might be the source of the confusion. You should not. Instead, show them the DisplayName property, and then save the corresponding Id.

For example, if you are creating an HTML <select> list, each <option> tag should have the Id put in the value attribute, and the DisplayName put in the option element's body.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matt Johnson-Pint