Sort List of Transform based on world position?

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);
        }
    }

This did the trick to sort them in a single array, but I still need to sort them in a matrix:

public void SortItems()
    {
        if (selectedItems.Count > 1)
        {
            Transform[] items= new Transform[selectedItems.Count];
            items= selectedItems.OrderBy(item=> GridRanking(item.transform.position)).ToArray();
            Debug.Log(items[0].name);
        }
    }

    private float GridRanking(Vector3 pos)
    {
        return pos.x - pos.z;
    }

Are you certain you meant List<List>?

Are you trying to sort a jagged list? Because that adds a lot of complexity to sorting.

Left upper corner of what?

Since we don’t have your jargon defined, we can’t see what it is you’re talking about.

This example code here doesn’t sort, but rather attempts to find the first most that meats your requirements.

Which you could use linq and the orderby routine to…

you just posted again while I was typing this, and it appears you found the orderby method.

1 Like

Kind off, still does not do what I need.

Are you expecting this matrix to be that jagged List<List> you talked about before?

1 Like

Well, I am opened to better options and solutions.

I have never done something like this in Unity and I just thougt it could work

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.

| a, b, c |
| d, e, f |
| g, h, i |

And by the way, thank you for helping