- Home /
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]
Answer by cjdev · Aug 17, 2015 at 09:30 AM
You could use some LINQ like this:
ObjList = ObjList.OrderBy(x => int.Parse(x.transform.name.Replace("number", ""))).ToList();
Answer by NeverHopeless · Aug 17, 2015 at 09:38 AM
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.
Answer by stepan-stulov · Aug 17, 2015 at 01:20 PM
[Serializable]
public class PositionedGameObject
{
public GameObject gameObject;
public float gameObjectPosition;
}
// Later...
public List<PositionedGameObject> positionedGameObjects;
// Event later...
positionedGameObjects.Sort(ComparePositionedGameObjectByWhateves);
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.
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.
Your answer
Follow this Question
Related Questions
Show Top 10 Of 1 GameType 2 Answers
A node in a childnode? 1 Answer
Sorting Variables Help 1 Answer
store/rearrange integers for a basic scoreboard with C# 2 Answers