- Home /
Question by
bellwether · Sep 24, 2017 at 05:31 PM ·
listsortinglinq
sorting list by name
My list hold 13 elements and 'orderby' wont sort anything past 8 elementa. how do I fix that? The listed items names are 'Cube 01' and so on if that helps.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class dice_roll : MonoBehaviour {
public List<Transform> BoardPosition = new List<Transform>();
void Start()
{
foreach(GameObject fooObj in GameObject.FindGameObjectsWithTag("SquareBoardPosition"))
{
BoardPosition.Add ((fooObj).transform);
}
BoardPosition = BoardPosition.OrderBy(x=>x.name).ToList(); <-------not working.
}
Comment
Best Answer
Answer by Thaun_ · Sep 25, 2017 at 10:40 AM
Dont use .ToList() when you are assigning to the same one.
Try to use var newList = BoardPosition.OrderBy(x=>x.name).ToList();
then assign BoardPosition = (List<Transform>) newList;
I havent tested it.
I tried this and the outcome is the same with the numbers going 1,10,11,12,13,2,3,4,5,6,7,8,9
Answer by Bilelmnasser · Sep 26, 2017 at 02:11 PM
Hi, try this :
private static int SortByName(GameObject o1, GameObject o2) {
return o1.name.CompareTo(o2.name);
}
// later on...
myObjects.Sort(SortByName); // sorted by name now
Référence here in MSDN documentation