Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

328
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda