Menü Schließen

Using a Comparer to Sort CultureInfos by their Name

If you need to show a list of languages, or more general CultureInfos, you might want to sort them by the name they show up in the GUI.

This can easily be done by using a custom comparer. This implements the generic IComparer<T> interface.

public class CultureInfoComparer : IComparer<CultureInfo>
{
  public int Compare(CultureInfo x, CultureInfo y)
  {
    return (new CaseInsensitiveComparer().Compare(x.DisplayName, x.DisplayName));
  }
}

Usage:

var languages = new List<CultureInfo>();

// Add CultureInfos to the list

languages.Sort(new CultureInfoComparer());

 

Ähnliche Beiträge

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert