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.

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

@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);            
        }

}