我有以下课程:
class Detail
{
public Detail()
{
_details = new List<string>();
}
public IList<string> Details { get { return _details; } }
private readonly List<string> _details;
}
目前,我使用以下方法对类进行随机排序:
void ShuffleGenericList<T>(IList<T> list)
{
//generate a Random instance
var rnd = new Random();
//get the count of items in the list
var i = list.Count();
//do we have a reference type or a value type
T val = default(T);
//we will loop through the list backwards
while (i >= 1)
{
//decrement our counter
i--;
//grab the next random item from the list
var nextIndex = rnd.Next(i, list.Count());
val = list[nextIndex];
//start swapping values
list[nextIndex] = list[i];
list[i] = val;
}
}
我想要做的是按字母顺序对细节内容进行排序。
例如,如果内容是这样的:
[0] a
[1] d
[2] b
我希望能够运行这个方法,并将它们分类为:
[0] a
[1] b
[2] d
有人知道一个简单的方法吗?请注意,这些列表中通常只有不到10个条目。我能用 LINQ 做这个吗?对不起,我不是很熟悉 LINQ,我刚刚听到一个建议,我可以使用它。