私はこれを持っていますList<string>:
IList<string> ListaServizi = new List<string>();
アルファベット順および昇順で並べ替えるにはどうすればよいですか?
ListaServizi = ListaServizi.OrderBy(q => q).ToList();
ソートを使用できます
List<string> ListaServizi = new List<string>() { };
ListaServizi.Sort();
他の答えは正しい提案Sortですが、保存場所がとして入力されているという事実を見逃しているようですIList<string。 Sortインターフェイスの一部ではありません。
ListaServizi常にが含まれることがわかっている場合はList<string>、宣言されたタイプを変更するか、キャストを使用できます。よくわからない場合は、次のタイプをテストできます。
if (typeof(List<string>).IsAssignableFrom(ListaServizi.GetType()))
((List<string>)ListaServizi).Sort();
else
{
//... some other solution; there are a few to choose from.
}
おそらくもっと慣用的です:
List<string> typeCheck = ListaServizi as List<string>;
if (typeCheck != null)
typeCheck.Sort();
else
{
//... some other solution; there are a few to choose from.
}
ListaServiziそれが時々異なる実装を保持することを知っているならIList<string>、コメントを残してください、そして私はそれをソートするための提案を1つか2つ追加します。
ListaServizi.Sort();
あなたのためにそれをします。文字列のリストを使えば簡単です。オブジェクトを並べ替える場合は、もう少し賢くする必要があります。
List<string> myCollection = new List<string>()
{
"Bob", "Bob","Alex", "Abdi", "Abdi", "Bob", "Alex", "Bob","Abdi"
};
myCollection.Sort();
foreach (var name in myCollection.Distinct())
{
Console.WriteLine(name + " " + myCollection.Count(x=> x == name));
}
出力: アブディ 3 アレックス 2 ボブ 4