- Home /
List Sorting But Random Order
I couldn't quite find the correct method or what I was looking for so I will post if here even tho I really didn't want to.
I want to sort my list which I already do by a value but if the value is the same can it be added randomly in comparison with the other list items?
listOrder = listOrder.OrderByDescending(x => x.item.value).ToList();
//Say we have 3 items and value is 2,1,1 for them the list sort by adds them in always in the same order using value which means 2,1,1 never changes is there any way to have the 1,1 items be able to have there position randomly added when compared to each other because their value is the same?
I know it seems like a very simple question but I don't have a lot of experience using list sorting and compare to's and more knowledge on the matter will be beneficial to know.
Thanks for any help you can offer I appreciate it.
Answer by MacDx · Jun 07, 2018 at 09:56 PM
It's not clear what you want here. You cannot have something that's sorted and in random order, those properties are mutually exclusive. Do you want the list to be sorted or do you want it to be in a random order? If random order is what you want then just do a shuffle after you've added all the elements you want to the list.
There are many shuffling algorithms out there but one of the more popular ones is the Fisher - Yates Shuffle just add this script to your project:
using System;
using System.Collections.Generic;
public static class MyExtensions
{
private static readonly Random rng = new Random();
//Fisher - Yates shuffle
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
And then you can do:
listOrder.Shuffle();
And just like that you have list with randomized element positions.
Hope this helps!
Hmm not quite what i wanted the easiest way i can explain it is.
public List<Item> itemOrder = new List<Item>();
void SortItem(){
itemOrder = itemOrder.OrderByDescending(x => x.item.value).ToList();
//^ this sorts fine what i want but say we have these
itemOrder[0].value = 2;
itemOrder[1].value = 1;
itemOrder[2].value = 1;
//no matter how many times you run this the order will always be the //same as the above because there already ordered by value
but how can I get it to randomize the order but only for the values that are the same so i can either end up with
itemOrder[0].value = 2;
itemOrder[1].value = 1;
itemOrder[2].value = 1;
as above or
itemOrder[0].value = 2;
itemOrder[2].value = 1;
itemOrder[1].value = 1;
//or it can be this because [2] and [1] have the same value
}
is it possible to do that?
i know it seems redundant lol but the reason i need it because i want my character to take there turn based on speed but if there speed is the same i want the order to random both there turns and not every ones turn that way its 50/50 who gets to move on a speed tie first.
sorry i can't explain it better than that
Ahhhh I think I get what you mean now. Give me a $$anonymous$$ute to explain this
What you need to do is Shuffle certain ranges of your ordered list (a range of items that hold the same value in their value property lol)
In that case you'll first need to deter$$anonymous$$e the ranges of repeated values that exist on your list. You can do that easily by using LINQ. Then you need to shuffle every one of those ranges, meaning that the elements are ordered by speed but the order within the ranges of elements with the same speed is randomized.
//Assu$$anonymous$$g itemOrder is sorted at this point
//Find what values are repeated and how many times they're repeated
var repeatedSpeeds = itemOrder.GroupBy(x => x.value).Where(g => g.Count() > 1).Select(y => new {Value = y.$$anonymous$$ey, Count = y.Count()});
//Iterate over result. The anon type contains Value, and Count (repetition count).
foreach(var speed in repeatedSpeeds)
{
int startIndex = itemOrder.IndexOf(itemOrder.First( x => x.value == speed.Value));
int repetitionCount = speed.Count;
//Shuffle range
listOrder.ShuffleRange(startIndex,repetitionCount);
}
I didn't put the implementation of ShuffleRange here but it should be fairly easy to do.
Note: since you mentioned speed I'm equating the value property of your Item class to that.
Implementation of shuffle range
public static void ShuffleRange<T>(this IList<T> list, int startIndex, int count)
{
int n = startIndex + count;
int limit = startIndex + 1;
while (n > limit)
{
n--;
int k = rng.Next(startIndex,n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
I get it I see what you did that's very clever I tested it and it works perfectly thank you so much for answering that now I can start learning more about how it works.
I appreciate the time you took :)
There’s no such method as rng.Next() with Unity Random. Be sure to specify System.Random
Answer by suleymanbucuk · Aug 23, 2020 at 02:33 PM
@RKSlither I have some planets and want to instantiate them randomly at the beginning of my game. For randomly sorting every time game begins, i created two lists one is temporary list and the second is random list. I get a random number between 0 and templist.count and add that rand index to real list and remove templist rand index in a for loop till the temp list is empty. That worked for me, hope works for you too.
private List<GameObject> planetTempList = new List<GameObject>();
private List<GameObject> realPlanetList = new List<GameObject>();
private void Start()
{
int x = planetTempList.Count; // will use for "for loop"
for (int i = 0; i < x; i++)
{
int rand = Random.Range(0, planetTempList.Count);
realPlanetList.Add(planetTempList[rand]);
planetTempList.RemoveAt(rand);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
draw and compare 1 Answer