Search code examples
linqsortingunity-game-enginegame-development

How to Sort a List of Cameras by a Script in Unity using OrderByDescending?


I'm trying to make a list of cameras that sorts itself based on a number held by a script that each camera has. Why is this code not functioning?

cams = cams.OrderByDescending(cams => cams.GetComponent<cameraLocator>().cameraZone) ;

Camera Zone is the ID number I want to sort by, cams is the list to be sorted.


Solution

  • Your first attempt was almost correct. You still need to convert the IOrderdEnumerable back to a list.

    cams = cams.OrderByDescending(cams => cams.GetComponent<cameraLocator>().cameraZone).ToList();