Shuffling C# generic lists

Just a quick hack for shuffling generic lists in C# like this one:

List<T> list

You may use proper solutions like those in this StackOverflow thread, if you need it for something serious, or simply do this:

list.Sort ((x, y) => Random.value < 0.5f ? -1 : 1)

which simply defines a sorting function that randomly returns -1 or 1 when comparing two elements, shuffling the original list.

I’m not exactly sure if this is right, but it works! If there’s a reason why this is not a good idea, please share it!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.


This is PlayMedusa's game devlog: sketches, artwork, news, tutorials... Know more about us and our work in our MAIN SITE!

We create games mostly because we enjoy doing so. We hope you also enjoy playing them! Check them out at itchio Some can be played on the browser!

Este sitio web utiliza cookies para que usted tenga la mejor experiencia de usuario. Si continúa navegando está dando su consentimiento para la aceptación de las mencionadas cookies y la aceptación de nuestra política de cookies, pinche el enlace para mayor información.plugin cookies

Aviso de cookies