Good day, I need to fill a List<List> to make an alignment script.
To start with, I need to somehow detect wich is the item located in a corner. For this purpose I will chose the left upper corner, which should give me smaller x and smaller z values.
As you can see there is a matrix of rectangles, I am using an 2D camera to see every thing from above, the height of the rectangles is always the same (0.6f)
This is what I have so far but it does not work:
public void SortItems()
{
if (selectedItems.Count > 1)
{
Transform t = selectedItems[0];
foreach (Transform t1 in selectedItems)
{
if (t1.position.x < t.position.x && t1.position.z < t.position.z)
{
t = t1;
break;
}
}
// This line should print the name of the item located in the left upper corner
Debug.Log(t.name);
}
}
Well the big thing here is, how do you expect the matrix to sort?
Row first, than column?
From left to right and top to bottom, right to left bottom to top, left to right bottom to top… because it’s 2d, it can sort in so many different ways. So the idea of “sorting” isn’t exactly straight forward. You have to define this behaviour.
Also… you suggest a List of Lists… List<List>. This means it’s a jagged matrix. It may have a constant row count, but an inconsistent column count (or vice versa, depending the ordering you define your jagged list).
Like… what do you expect the matrix to look like once filled and sorted? Maybe draw it for us in ascii or paint.
//unsorted
| b, e, g |
| c, a, f |
| d, h, i |
//sorted row column
| a, b, c |
| d, e, f |
| g, h, i |
//sorted column row
| a, d, g |
| b, e, h |
| c, f, i |
The idea is that it shoult be sorted by any way as the use specifies, but to begin and for a prototype, I was looking to achieve exactly your first example row, column.