How to arrange list


I have two lists:

var PosList : List.<float>;
var ObjList : List.<GameObjects>;

I want to arrange the first list, and the second list to be arranged accordingly to the first one.

For example:

//list one contains:

//list two contains gameObjects with the names:

//I sort the first list, so I use:

//Now the first list became:

//How can I arrange the second list to mach the first one? //Like this:

You could use some LINQ like this:

ObjList = ObjList.OrderBy(x => int.Parse("number", ""))).ToList();

You should better use Dictionary<float, GameObject>. So once you call Sort, it will be sorted as per the key. Or you can try custom sorting procedures.

Also, there is a possibility to use SortedDictionary.

Using dictionary you don;t have to be precise about the value on each index, once the key sorted there corresponding value will be moved in the same way.

Hope you will not see this problem then.

public class PositionedGameObject
public GameObject gameObject;
public float gameObjectPosition;

// Later...

public List<PositionedGameObject> positionedGameObjects;

// Event later...

  1. What you have: you store a list of multi-purpose “complex” objects (a game object and its associated position) in a distributed manner in two lists which causes desyncing on top of being very unintuitive.
  2. What you actually want: create a class/struct that describes an object and its position together. Store a list of instances of that class/structure. Vuala, now try to have them unsynced. You can’t.