Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

332
Vistas
How to sort Dictionary with Vector3 keys?

Scenario image

I am recreating a edge-link system from the game "Rimworld".

The purple squares are individual squares that need to form "links", in the direction the arrows are drawn in, with that exact length.

Currently, I don't know how to do this better than by placing all of those squares in a dictionary where I mark a square's position and its direction (which can be UP or RIGHT), like this:

Dictionary<Vector3Int, bool> linkSquares;

I want to sort this dictionary so that the KV pairs that come first are the ones where the Vector3Int.x is lowest and then where the Vector3Int.y is lowest.

That way, I could always start at those squares (marked with numbers 1-4 on the picture) and go in the appropriate arrow direction, removing those squares as I go, which would only leave other starting squares.

I've read that dictionaries can't be sorted, but I don't know how I would use a SortedDictionary for this case. This whole system seems overly dirty but I don't know how else I would achieve my goal here, so any design change suggestions are highly appreciated.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Try to define a custom class which includes the vector3 and bool together and then make list of objects from that class. Then you can sort the class in any you want.

If you only need the bool for filtering then you can just sort the dictionary into a list. An example of sorting dictionary into list is as below:

List<Vector3Int> productList = myDictionary.OrderBy(kp => kp.key.x).ThenBy(kp => kp.key.y)
                                      .ToList();

Check this, and this for more sample codes.

over 4 years ago · Santiago Trujillo Denunciar

0

you could use OrderBy for the first key and ThenBy for the second key (Linq) and put the result in new dictionary (sorted):

        var dictsorted= linkSquares.OrderBy(k => k.Key.x).ThenBy(k => k.Key.y).ToDictionary(k => k.Key, k=> k.Value);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda