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 Else
Then next time, it could be:
- Some Other Name
- Some Name
- Something Else
Know 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.
Like this:
If you want the list to be sorted by the random rank:
However, this is not a valid ordering (
a < bdoes not implyb > a) and may cause unexpected results.