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): //Both arrays have a size of 4, and enemy_distance is filled as soon as the game is played by radar script public float[] enemy_distance; public float[] distance_ordered; void Update() { //Here is where I lost it. I tried Array.Sort and everything but it didn't work. } Thank you very much for your time, I really appreciate it!
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.
You just clone the first array into the second array and then sort it after that. Code (CSharp): public float[] enemy_distance; public float[] distance_ordered; void Update() { distance_ordered = (float[])enemy_distance.Clone(); Array.Sort(distance_ordered); }
I think you should use lists instead of arrays. Unless you're doing some serious performance optimization, you rarely need to use arrays.
Something like this: Code (CSharp): public List<float> enemy_distance; public List<float> distance_ordered; void SortLists() { { enemy_distance.Sort(); distance_ordered.Sort(); } 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.
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()?
Why not use LINQ? Something like (untested!) Code (csharp): #include System.Linq; float[] distance_ordered = enemy_distance.OrderBy(i => i).ToArray(); should work.
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.
Why bubble sort? I think merge/quick sort is more widely used these days. Always good to learn about more algorithms, though
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): elements.Sort((x, y) => x.priority.CompareTo(y.priority)); 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]