Search Unity

OrderByDescending Help

Discussion in 'Scripting' started by renman3000, Mar 31, 2015.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Hi,
    I am trying to sort a list of objects, unit, by thier y position but am having difficulty using the C# list method of OrderByDescending.


    This is an example of it I found.
    Code (csharp):
    1.  
    2.  List<GameObject> sortedbydist = listofguys.OrderByDescending(x => x.Value).Select(p => p.Key).ToList();

    I am unsure how to apply this to an existing list of class unit, accessing the transform of each, y position.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Code (csharp):
    1.  
    2. OrderByDescending(unit => unit.transform.position.y)
    3.  
     
    PersianKiller and renman3000 like this.
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Thanks KesloMRK,
    quick question tho, clearly this logic (what I have) will not work. It is not incorporating each unit of the list.
    Code (csharp):
    1.  
    2. list_unitsYPos_order = answerUnits.OrderByDescending(answerUnits => answerUnits.transform.position.y);

    Where answerUnits = list of count x.
     
  4. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    That's saying that you're using the same name "answerUnits" as a variable name and a lambda variable name. Change it to something like:
    Code (csharp):
    1. list_unitsYPos_order = answerUnits.OrderByDescending(au => au.transform.position.y);
     
    renman3000 likes this.
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Ok, so essentially, this method(), tho I think technically may be an extension.... it is running thru all in the list answerUnits.

    But, I have a couple of questions,
    au, this is what? Obviously it needs to equate to an answerUnits. But where I am confused is, since this is not runnig thru a for(), how is the method gathering each answerUnits?

    PS. I have not tested, as I was leaving.
    Will test.
    Thanks!
     
  6. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Well, I would suggest reading about LINQ ( https://msdn.microsoft.com/en-us/library/bb397933.aspx ) and Lambda Expressions ( https://msdn.microsoft.com/en-us/library/bb397687.aspx ).

    Basically LINQ methods, like OrderByDescending, operate on lists (any IEnumerable actually) and return searched/reordered/transformed versions. Lambda expressions are of the form "x => DoSomething(x)" and translate to "given a variable x, do this to it". It's like a mini function where the parameter names are on the left side and the function body is on the right side. There's more to it than that but those are the basics.
     
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Ok, so I do not need to declare au, it just knows that this is a member of the original list?
     
  8. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Yes, the variable name on the left side of the lambda "=>" is sort of auto-declared, and it figures out what type it is by the statement that it's in; in your case the type is "GameObject", which it knows because you are calling OrderByDescending on a list of GameObjects.
     
  9. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    To make it more clear, when you call someList.OrderByDescending(thing => thing.someNumber), it is saying "go through someList, and for each element, which we'll call "thing", get thing.someNumber, and use that value to decide what order to put the list in".
     
    Goldorak5 likes this.
  10. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Right, I get that.

    However, I am still struggling with the logistics.....

    Code (csharp):
    1.  
    2. list_unitsYPos_order = answerUnits.OrderByDescending(au => au.transform.position.y);
    3.  

    edit, just so you know, answerUnits is a list of Answer_Unit class. list_unitsYPos is an empty list of float class.
     
  11. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Ah, you need to put "using System.Linq;" at the top of the file.
     
    chaiwatketsuwan and dick like this.
  12. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Ahhhh.
    Merci boucoup.

    :)(
     
  13. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Ah, but I am getting another error.

    First, it mentioned that I can not convert a float to a someClass. So I changed the class of the secondary list, from float, to someClass, but still an error.


     
  14. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If you're assigning the result then you need to add ToList(); as the last part of the chain.
     
  15. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Awesome.
    Seems to work.

    Thank you.

    :)
     
  16. Goldorak5

    Goldorak5

    Joined:
    Sep 7, 2023
    Posts:
    1
    seriously best explaination i found yet thanks