Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Sort List

Discussion in '2D' started by DrDress, May 13, 2020.

  1. DrDress

    DrDress

    Joined:
    Sep 11, 2019
    Posts:
    30
    I'm making a list of the following objects:

    Code (CSharp):
    1.  
    2. public class Tile
    3. {
    4.     public int x;
    5.     public int y;
    6.     public float pathCost;
    7.     public Tile pathLink;
    8. }
    9.  
    I want to sort the list by pathCost using this code:

    Code (CSharp):
    1.  
    2. List<Tile> tileQueue = new List<Tile>();
    3.         tileQueue.Add(start);
    4.  
    5.         for(int d =0; d<10; d++)
    6.         {
    7.             Tile dims = new Tile(d, d, TileSurfaceType.Desert, TileFeatureType.None);
    8.             dims.pathCost = UnityEngine.Random.Range(0, 100);
    9.             tileQueue.Add(dims);
    10.         }      
    11.         List<Tile> tileQueOrdered = tileQueue.OrderBy(o => o.pathCost).ToList();
    12.        
    I get this error as if OrderBy isn't a method for my list. I figured all lists have these Order methods. Is this some Unity thing? I have had it working before in plain c#.

    Code (CSharp):
    1. Severity    Code    Description    Project    File    Line    Suppression State
    2. Error    CS1061    'List<Tile>' does not contain a definition for 'OrderBy' and no extension method 'OrderBy' accepting a first argument of type 'List<Tile>' could be found (are you missing a using directive or an assembly reference?)    Assembly-CSharp    C:\Users\permy_000\Dropbox\Per\Vejen til spiludvikler\Repos\Unity 2D\Resource Allocation\Resource Allocation\Assets\Scripts\GridController.cs    279    Active
    3.  
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    OrderBy is part of System.Linq. Try adding
    using System.Linq;
    at the top of your page. This will allow you to make use of Linq.

    But since this looks like some kind of pathfinding code, if it's in a hot path you shouldn't use Linq because it can be non-performant. You might instead use myList.Sort().

    tileQueue.Sort((a, b) => a.pathCost.CompareTo(b.pathCost)); 


    That might work and removes the usage of Linq.
     
  3. DrDress

    DrDress

    Joined:
    Sep 11, 2019
    Posts:
    30
    Wow. Great reply. Thanks, I'll try that.

    What is a hot path, though?
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    Most of your code is cold: it executes very rarely. Often pathfinding has places which may do expensive operations over and over again. These could be considered hot. The Profiler is the best way to figure out what is actually a performance concern. In general, many people avoid Linq in games because if used injudiciously it can generate lots of avoidable garbage and can be a pain to go back and remove later. That said, I like Linq and use it wherever I'm sure that something happens infrequently.