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. Dismiss Notice

Question Please Help Explain my Log to me.

Discussion in 'Scripting' started by Mortalanimal, May 14, 2020.

  1. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    558
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class LearningScript : MonoBehaviour
    7. {
    8.     List<SpeedOrgeniser> ListName = new List<SpeedOrgeniser>();
    9.  
    10.  
    11.     public class SpeedOrgeniser : IComparable<SpeedOrgeniser>
    12.     {
    13.         public string name;
    14.         public int combatSpeed;
    15.  
    16.  
    17.         public SpeedOrgeniser(string unit, int newCombatSpeed)
    18.         {
    19.             name = unit;
    20.             combatSpeed = newCombatSpeed;        
    21.         }
    22.  
    23.         public int CompareTo(SpeedOrgeniser other)
    24.         {
    25.             if (other ==null)
    26.             {
    27.                 return 1;
    28.             }
    29.             Debug.Log(other.combatSpeed - combatSpeed);
    30.             return  other.combatSpeed - combatSpeed;
    31.         }
    32.     }
    33.  
    34.     void Start()
    35.     {          
    36.         ListName.Add(new SpeedOrgeniser("player1", 150));
    37.         ListName.Add(new SpeedOrgeniser("player2", 121));
    38.         ListName.Add(new SpeedOrgeniser("player3", 121));
    39.         ListName.Add(new SpeedOrgeniser("player4", 123));    
    40.  
    41.         ListName.Sort();
    42.  
    43.         foreach (SpeedOrgeniser item in ListName)
    44.         {
    45.             print(item.name + "     " + item.combatSpeed);
    46.         }
    47.     }
    48. }




    What Confuses me is;
    1) why did "-2" run twice?
    2) why is it " positive 27" and not "-27"
    3) what does "return 1" even mean? I have used "return" in loops before, but what does "return 1" do here?

    I would love if you can explain to me step by step what is going on here. Many thanks
     
  2. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    558
    I think I get it, some of it;

    Player 1 - Player 2 = 29
    Player 2 - Player 3 = 0
    Player 2 - Player 4 = -2 (because Player 2 and 3 had same value, it moves again)
    Player 3 - Player 4 = -2
    Player 1 - Player 4 = 27 ( Not sure why this is not Player 4 - Player 1) Something to do with the return? Nop, the Return If Statement returns false anyway with this example
     
    Last edited: May 14, 2020
  3. ArinFaraj

    ArinFaraj

    Joined:
    Jan 5, 2014
    Posts:
    13
    first it looks at the 2nd item and compares it to the 1st one, because the return value was positive that means it's smaller and it shouldn't go higher
    then goes to 3rd and the return is 0 cause no difference between 2 and 3 so it stays in its place

    then comes the 4th one and the value becomes negative (-2) so it should go higher, then it will compare it again with 2nd one so (-2) again and it will go higher
    finally it will be smaller than the 1st one so it's done
     
  4. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    558
    Wait i thought I got it, But I dont, How do you explain the reverse;


    Code (CSharp):
    1. public int CompareTo(SpeedOrgeniser other)
    2.         {
    3.             if (other ==null)
    4.             {
    5.                 Debug.Log("Does this ever Run");
    6.                 return 1;
    7.             }
    8.             Debug.Log(other.combatSpeed - combatSpeed);
    9.             return    combatSpeed - other.combatSpeed;
    10.         }
    11.  
    12.  
     
  5. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    558
    I am still so confused, can you show me mathematically every step to reflect the logs? Many thanks
     
  6. ArinFaraj

    ArinFaraj

    Joined:
    Jan 5, 2014
    Posts:
    13
    1st value does not need to be checked, other items move around it.
    check for 121
    121 - 150 = -29 so 121 goes higher
    check for other 121
    121 - 150 = -29 cause 150 is now in the 2nd place, and so 121 goes higher
    121 - 121 = 0 done
    check for 123
    123 - 150 = -27 moves higher
    123 - 121 = +2 so everything is sorted

    btw if u want it reversed use
    list.sort();
    list.reverse();
    don't do it in compare
     
    Last edited: May 21, 2020
    Mortalanimal likes this.
  7. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    558

    Thank you very much!!