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

How to go about sorting an array of floats in ascending order...

Discussion in 'Scripting' started by FuzzyQuills, Sep 19, 2014.

  1. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Hi guys.

    New problem for me: I am attempting something that probably isn't advised, but I am attempting to sort a list of floats (computed with Vector3.Distance()) in ascending order so I can pick the shortest one out and use it for a light probe system I am making.

    Only problem is, I have run out of approaches, as it appears using System.Array.Sort() throws InvalidCastException, EVEN if the type it's comparing is float!

    Any ideas? here's the IComparer code:
    Code (JavaScript):
    1. class UpdateGI implements IComparer
    2. {
    3.     function Compare(a : System.Object, b : System.Object) : int {
    4.         var A : float = a;
    5.         var B : float = b;
    6.        
    7.         if (A < B){
    8.             return B;
    9.         }
    10.         else {
    11.             return A;
    12.         }
    13.     }
    14. }
     
  2. GBeats

    GBeats

    Joined:
    May 22, 2014
    Posts:
    4
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,936
  4. Krysalgir

    Krysalgir

    Joined:
    Aug 30, 2010
    Posts:
    95
  5. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    @mgear: will look into this. I did solve my issue in the end, and it was due to the compare function.
    simply using array.sort() like this:
    Code (csharp):
    1. System.Array.Sort(<Array of floats to sort>);
    Works, but it doesn't know what the value belongs to. again, thank you!

    at everyone: thank you for all your help, guys! :)
     
  6. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    You can also do System.Array.Sort<float>(<Array of floats to sort>). Saves implementing a custom comparer
     
  7. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    @TwixEmma: I am actually doing that now, although a bit differently! Here is how I am doing it:
    System.Array.Sort( <the array I wish to sort by distance>, <the array with distance values> );

    BTW, you actually a girl? :eek: A female game dev's a rare breed around here... ;) Also, if you are wondering why you asked this, it's the profile pic that got me... :D
     
  8. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Ok, this might be off-topic, but now I have a new problem: I can bake the cubemaps into the lightprobes, but because this is done in play mode, as soon as I exit, the cubemaps save, but they are no longer assigned to their probes!

    Any help with a method of keeping them assigned on exit would be great! :)
     
  9. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    Total off-topic and irrelevant, however to answer your question, yes I am a girl and no I'm not interested! <3
     
  10. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    @TwixEmma:
    WHOA! Sorry! I didn't mean it like that! :eek:
     
  11. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Have a look at LINQ OrderBy.
     
  12. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    @A.Killingbeck: I actually fixed the problem somewhat. Again, it's doing a double sort, by using one array as a set of keys to sort another.

    I also got the lighting system working a different way by using trigger colliders. ;) And LINQ... where have I heard that before? :D
     
  13. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    @A.Killingbeck: I actually fixed the problem somewhat. Again, it's doing a double sort, by using one array as a set of keys to sort another.

    I also got the lighting system working a different way by using trigger colliders. ;) And LINQ... where have I heard that before? :D