Search Unity

Sorting an int Array

Discussion in 'Scripting' started by GoodNight9, Sep 2, 2016.

  1. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Hello everyone!!

    I have a small array of 4 floats. I'm looking for a way to sort it so that the new array has all the values of the old one but with the smallest values at the top, and the largest values at the bottom.

    I've been looking for a while, but I can't seem to find out how.
    Here is basically what I have so far:

    Code (csharp):
    1.  
    2. //Both arrays have a size of 4, and enemy_distance is filled as soon as the game is played by radar script
    3. public float[] enemy_distance;
    4. public float[] distance_ordered;
    5.  
    6. void Update()
    7. {
    8.     //Here is where I lost it. I tried Array.Sort and everything but it didn't work.
    9. }
    10.  
    Thank you very much for your time, I really appreciate it!
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    If you use a List, you can just use the sort call on it. so, myList.Sort(); would work on a list.

    Or, if you want to keep your Array, just do

    Array.Sort(enemy_distance);
    You will have to add using System I think for this one.
     
    aszakmeister and GoodNight9 like this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
    You just clone the first array into the second array and then sort it after that.

    Code (CSharp):
    1. public float[] enemy_distance;
    2. public float[] distance_ordered;
    3.  
    4. void Update()
    5. {
    6.     distance_ordered = (float[])enemy_distance.Clone();
    7.     Array.Sort(distance_ordered);
    8. }
     
    GoodNight9 likes this.
  4. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    I think you should use lists instead of arrays. Unless you're doing some serious performance optimization, you rarely need to use arrays.
     
    GoodNight9 likes this.
  5. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Thank you everyone, I'll try that.

    How would the following code above look like as a list then??
     
  6. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Something like this:

    Code (CSharp):
    1. public List<float> enemy_distance;
    2. public List<float> distance_ordered;
    3.  
    4. void SortLists() {
    5. {
    6.     enemy_distance.Sort();
    7.     distance_ordered.Sort();
    8. }
    More on C# Lists. The primary difference is that you always just Add() new elements to the end of the list, and it dynamically resizes when full, so the size isn't static.
     
    JoeStrout and GoodNight9 like this.
  7. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    I can see the perks of using a list now, thank you Boz0r for the link it was really helpful!

    Thank that actually did exactly what I was trying to do. I have one hypothetical question though. If I had wanted the clone array to order itself from largest to smallest value instead of smallest to largest. What would have changed in the 2 lines of code in the Update()?
     
  8. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    There is an Array.Reverse(distance_ordered); that would flip it around to descending.
     
    GoodNight9 likes this.
  9. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Why not use LINQ?
    Something like (untested!)
    Code (csharp):
    1.  
    2. #include System.Linq;
    3.  
    4. float[] distance_ordered = enemy_distance.OrderBy(i => i).ToArray();
    5.  
    should work.
     
  10. GrischaG

    GrischaG

    Joined:
    Apr 26, 2013
    Posts:
    40
    You can look for sort algorithms like bubble sort anyways. It´s a good practice and more then nice to know.
    For example if you have a list with your own classes in it and want to sort it with other criterias that aren´t offered by lists.
     
  11. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Why bubble sort? I think merge/quick sort is more widely used these days. Always good to learn about more algorithms, though :)
     
  12. GrischaG

    GrischaG

    Joined:
    Apr 26, 2013
    Posts:
    40
    Yes, bubble sort was just an example ;)
     
  13. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    Sorry for resurrecting such an old topic. Just stumbled upon it and thought I might add my 2 cents in case some beginner finds this as well and might be sent on the wrong track

    I'm not sure that there's many everyday cases you really need to use your own bubble sort anymore (which in all likelihood will be way less efficient and lack loads of the comforts lists provide you). I've been working as a professional developer for about 8 or 9 years or so and I think the last time I encountered the need for my own bubble sort was back at university because it was just part of a class.

    With C# you have a lot of mighty tools on your hands. E.g. you could implement IComparable and lists even allow for things like this:
    Code (csharp):
    1.  
    2. elements.Sort((x, y) => x.priority.CompareTo(y.priority));
    3.  
    It's really hard for me to imagine a situation where you'd need to 100% implement your own sorting and whenever you can leverage a language feature instead of reinventing the wheel, you should probably do so unless you have an incredibly rare case and/or are operating on such a high level that you a) are able to implement things better than the people developing C# itself and b) have a need for a better implementation.[/CODE]
     
    Sagex likes this.