I’m trying to sort a list of int[,]'s by the first value of each array(int[0,0]). I thought I finally had it but now when I check the log, I keep getting the same value for each entry in the list. What am I doing wrong?
For sort object in your case applied OrderBy() from System.Linq. I write small example(write on CSharp):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class ListTest: MonoBehaviour {
public List<int[,]> myList = new List<int[,]>();
void Start() {
//Create int[,] array and add it into list
int[,] tp = new int[1, 2];
tp[0, 0] = 2;
tp[0, 1] = 3;
myList.Add(tp);
tp = new int[1, 2];
tp[0, 0] = 1;
tp[0, 1] = 4;
myList.Add(tp);
//And sorting
myList = myList.OrderBy(x=>x[0, 0]).ToList();
//And see
for(int i = 0; i < myList.Count; i++) {
Debug.Log("Element is " + i + " is [0, 0] = " + (myList*)[0, 0]);*
} } } I hope that it will help you. P.S.: If you want use method Sort, than instead line myList = myList.OrderBy(x=>x[0, 0]).ToList(); Write this code: myList.Sort(delegate(int[,] a, int[,] b) { return a[0, 0].CompareTo(b[0, 0]); });