- Home /
Sort list game objects based on name
I have created list of game objects those represent tile for my game. Based on game play dynamically tiles added or removed from list. I have created tile list using following code so that you can understand which class I have used to manage list.
private List<GameObject> tileList = new List<GameObject>();
Now I want to retrieve all tiles but in sorted order based on name. I have given game objectname in following manner tile01, tile02, tile03, .. etc.
Please give some help for this.
here is the start.
List.Sort http://msdn.microsoft.com/en-us/library/b0zbh7b6(v=vs.110).aspx
Answer by Landern · Nov 19, 2014 at 12:11 PM
Using your example:
using System.Linq;
using System.Collections.Generic;
public class MyClass
{
private List<GameObject> tileList = new List<GameObject>();
public void Start()
{
tileList = tileList.OrderBy(tile => tile.Name).ToList(); // done
// or the other way
//tileList = tileList.OrderByDescending(tile => tile.Name).ToList(); // done
}
}
Answer by staff0rd · Nov 19, 2014 at 12:23 PM
Providing you have;
using System.Linq;
in your using declarations, then you could use this;
var orderedByName = tileList.OrderBy(p => p.name);
Answer by DannyMen · Feb 01, 2018 at 03:32 PM
Okay this is the way:
// we are using the LINQ sytem: using System.Linq;
// create a list of gameObejcts, the objects must have a tag in the inspector
public List myList= new List(); // In the moment we Add the objects to the list we must .OrderBy name like below
myList.AddRange(GameObject.FindGameObjectsWithTag("theTAGofObject").OrderBy( go => go.name));
//HOPE THIS HELP SOMEONE
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Calling GameObject's transform from list 1 Answer
Return a gameObject from LINQ .GroupBy() 1 Answer
[C#] Sorting a List of Gameobjects Alphabetically 2 Answers
Sorting a List of Dictionaries in C#? 4 Answers