Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to Sort Integers and Equal Each One to Another Integer?

Discussion in 'Scripting' started by murtcnnbilgic, May 31, 2020.

  1. murtcnnbilgic

    murtcnnbilgic

    Joined:
    Jun 29, 2019
    Posts:
    21
    I have 7 different integers which change during game. I want to pick the highest integer's name and display it on the screen as the "1st place: name of the 1st integer" and second highest integer's name as "2nd place: name of the 2nd integer" etc. How do I do that? I don't want to write 7! if statements. Isn'T there a method or function for it?
     
  2. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    You can put all the int in an array or list and then sort the array and revers it so is high to low.and then you can have an array of string with 1st 2nd etc than loop through the second array and set 1st 2nd etc.
     
    Last edited: May 31, 2020
    murtcnnbilgic likes this.
  3. murtcnnbilgic

    murtcnnbilgic

    Joined:
    Jun 29, 2019
    Posts:
    21
    Thank you. I put all floats in a list with a name and value, and in Update() I tried to sort them. And oddly enough it nearly worked without errors. Now it sorts them from highest to lowest except it places the 3rd value to the 4th place but every other value is placed correctly.Here is the sorting code:
    Code (CSharp):
    1. for (int i = 0; i < partiOranlariList.Count; i++)
    2.         {
    3.             for (int j = i + 1; j < partiOranlariList.Count; j++)
    4.             {
    5.                 if (partiOranlariList[j].Oran > partiOranlariList[i].Oran)
    6.                 {
    7.                     float tmp = partiOranlariList[i].Oran;
    8.                     string tmpstring = partiOranlariList[i].Ad;
    9.                     partiOranlariList[i].Oran = partiOranlariList[j].Oran;
    10.                     partiOranlariList[i].Ad = partiOranlariList[j].Ad;
    11.                     partiOranlariList[j].Oran = tmp;
    12.                     partiOranlariList[j].Ad = tmpstring;
    13.                 }
    14.             }
    15.         }
     
  4. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Code (CSharp):
    1. int x = 2;
    2.         int y = 3;
    3.         int z = 5;
    4.             int[] T = new int[3];
    5.             string[] places = new string[3];
    6.             T[0]= z;
    7.             T[1] =x;
    8.             T[2] =y;
    9.             places[0] = "1st";
    10.             places[1] = "2nd";
    11.             places[2] ="3rd";
    12.             Array.Sort(T);
    13.             Array.Reverse(T);
    14.             for(int i =0;i<places.Length;i++)
    15.             {
    16.                 Console.WriteLine(places[i]+" "+T[i].ToString());
    17.             }
    I did that on my phone and is working fine maybe you could make similar to this ?
     
    murtcnnbilgic likes this.
  5. murtcnnbilgic

    murtcnnbilgic

    Joined:
    Jun 29, 2019
    Posts:
    21
    Just now I realized I misplaced 3th and 4th places, I changed them and it is working fine now, although your version is way more simple and comfortable to write. I will probably use something like that now on. Thank you.
     
  6. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    That's good .
    And simple code simple to debug too
     
    murtcnnbilgic likes this.