所以我有以下通用列表:
var topTenSomething = new List<Something>();这是件事:
public class Something { public string Name { get; set; } public int Rank { get; set; } }所以我想随机分配“Rank”属性,但需要从集合中的1个项目中进行排序。
因此,如果集合有3个项目,我想随机分配从1到3的排名:
一些名字 其他一些名字 还有别的然后下一次,它可能是:
其他一些名字 一些名字 还有别的明白我的意思了吗?
不知道怎么做 - 任何想法?
这是一个简单的研发原型 - 所以不要担心性能/为什么我这样做。 (真实的将具有由数据库分配的等级)
对LINQ /非LINQ版本感到满意 - 只要它有效。
So i have the following generic list:
var topTenSomething = new List<Something>();Here is Something:
public class Something { public string Name { get; set; } public int Rank { get; set; } }So i want to randomly assign the "Rank" property, but it needs to be ordered from 1-number of items in the collection.
So if the collection has 3 items, i want to randomly assign ranks from 1 to 3:
Some Name Some Other Name Something ElseThen next time, it could be:
Some Other Name Some Name Something ElseKnow what i mean?
Not sure how to do it - any ideas?
This is for a simple R&D prototype - so don't worry about performance/why i am doing this. (the real one will have rank assigned by database)
Happy with either a LINQ/non-LINQ version - as long as it works.
最满意答案
喜欢这个:
var rand = new Random(); var sequence = Enumerable.Range(0, list.Count).OrderBy(i => rand.Next()).ToList(); for(var i = 0; i < list.Count; i++) list[i].Rank = sequence[i];如果您希望列表按随机排名排序:
var rand = new Random(); list.Sort((a, b) => rand.Next(-1, 2)); //Exclusive upper bound for(var i = 0; i < list.Count; i++) list[i].Rank = i;但是,这不是有效的排序( a < b并不意味着b > a )并且可能导致意外结果。
Like this:
var rand = new Random(); var sequence = Enumerable.Range(0, list.Count).OrderBy(i => rand.Next()).ToList(); for(var i = 0; i < list.Count; i++) list[i].Rank = sequence[i];If you want the list to be sorted by the random rank:
var rand = new Random(); list.Sort((a, b) => rand.Next(-1, 2)); //Exclusive upper bound for(var i = 0; i < list.Count; i++) list[i].Rank = i;However, this is not a valid ordering (a < b does not imply b > a) and may cause unexpected results.
C#通用列表所以我有以下通用列表:
var topTenSomething = new List<Something>();这是件事:
public class Something { public string Name { get; set; } public int Rank { get; set; } }所以我想随机分配“Rank”属性,但需要从集合中的1个项目中进行排序。
因此,如果集合有3个项目,我想随机分配从1到3的排名:
一些名字 其他一些名字 还有别的然后下一次,它可能是:
其他一些名字 一些名字 还有别的明白我的意思了吗?
不知道怎么做 - 任何想法?
这是一个简单的研发原型 - 所以不要担心性能/为什么我这样做。 (真实的将具有由数据库分配的等级)
对LINQ /非LINQ版本感到满意 - 只要它有效。
So i have the following generic list:
var topTenSomething = new List<Something>();Here is Something:
public class Something { public string Name { get; set; } public int Rank { get; set; } }So i want to randomly assign the "Rank" property, but it needs to be ordered from 1-number of items in the collection.
So if the collection has 3 items, i want to randomly assign ranks from 1 to 3:
Some Name Some Other Name Something ElseThen next time, it could be:
Some Other Name Some Name Something ElseKnow what i mean?
Not sure how to do it - any ideas?
This is for a simple R&D prototype - so don't worry about performance/why i am doing this. (the real one will have rank assigned by database)
Happy with either a LINQ/non-LINQ version - as long as it works.
最满意答案
喜欢这个:
var rand = new Random(); var sequence = Enumerable.Range(0, list.Count).OrderBy(i => rand.Next()).ToList(); for(var i = 0; i < list.Count; i++) list[i].Rank = sequence[i];如果您希望列表按随机排名排序:
var rand = new Random(); list.Sort((a, b) => rand.Next(-1, 2)); //Exclusive upper bound for(var i = 0; i < list.Count; i++) list[i].Rank = i;但是,这不是有效的排序( a < b并不意味着b > a )并且可能导致意外结果。
Like this:
var rand = new Random(); var sequence = Enumerable.Range(0, list.Count).OrderBy(i => rand.Next()).ToList(); for(var i = 0; i < list.Count; i++) list[i].Rank = sequence[i];If you want the list to be sorted by the random rank:
var rand = new Random(); list.Sort((a, b) => rand.Next(-1, 2)); //Exclusive upper bound for(var i = 0; i < list.Count; i++) list[i].Rank = i;However, this is not a valid ordering (a < b does not imply b > a) and may cause unexpected results.
发布评论