How to arrange list

Hello,

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:
[3]
[1]
[2]

//list two contains gameObjects with the names:
[number3]
[number1]
[number2]

//I sort the first list, so I use:
PosList.Sort();

//Now the first list became:
[1]
[2]
[3]

//How can I arrange the second list to mach the first one? //Like this:
[number1]
[number2]
[number3]

You could use some LINQ like this:

ObjList = ObjList.OrderBy(x => int.Parse(x.transform.name.Replace("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.

[Serializable]
public class PositionedGameObject
{
public GameObject gameObject;
public float gameObjectPosition;
}

// Later...

public List<PositionedGameObject> positionedGameObjects;

// Event later...

positionedGameObjects.Sort(ComparePositionedGameObjectByWhateves);
  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.