【发布时间】:2011-08-06 07:04:30
【问题描述】:
我有以下课程:
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
有没有人知道一个简单的方法来做到这一点?请注意,列表中的条目通常少于十个。我可以用 LINQ 做到这一点吗?抱歉,我对 LINQ 不是很熟悉,我刚刚听到一个建议,我可以使用它。
【问题讨论】: