- Home /
Sorting an array - C#
Hi, I'm trying to make a ranking with the best scores. All scores are stored in an array, but when I try to sort the values to make a ranking, Unity returns the following message:
Assets/Scripts/Test.cs(12,29): error CS1501: No overload for method Sort' takes
0' arguments and Assets/Scripts/Test.cs(12,27): error CS0103: The name `Array' does not exist in the current context
I looked for answers in Unity Answers, Unify Community, MSDN Library, but still doesn't work. Is possible sort values in Unity/C#?
My Code:
public int[] playerScore = new int [] {35, 53, 32, 27, 16, 72, 44, 83, 51, 91};
// Use this for initialization
void Start () {
playerScore.Sort();
}
and another try:
public int[] playerScore = new int [] {35, 53, 32, 27, 16, 72, 44, 83, 51, 91};
public int[] ranking;
// Use this for initialization
void Start () {
ranking = Array.Sort ( playerScore );
}
Thanks guys!!
I just wanted to comment for other people that run across this Q: you shouldn't be using Arrays in C# for this kind of table data. ArrayLists or even better; List T are much better for non-static data. For ListT you do need to add using System.Collections.Generics
List is the current recommended List data structure. However, if you have a REALLY (1$$anonymous$$b+) large list of data, and it's length doesn't change, arrays are your best choice. The list system adds some overhead, and over big data sets you will have slowdowns.
Arrays are always faster than Lists, even if the data set isn't large, especially for types such as int. (Accessing int[]
is 6X faster than List<int>
, but with complex types such as GameObject, there's not much difference.) If the array doesn't need to change size, then use arrays and not Lists. Especially don't use ArrayList, unless the array contains data of more than one type, but that's something you'd rarely do.
Lists can actually work for multi-type data, as long as the data is all subclassed and polymorphic, but that comes down to having a single type, just through a layer of abstraction.
Answer by Marnix · Jun 26, 2011 at 06:51 PM
This works just fine.
using System;
namespace TestProject
{
class Program
{
static void Main(string[] args)
{
int[] one = { 5, 78, 68, 987, 5, 3, 6, 4 };
Array.Sort(one);
for (int i = 0; i < one.Length; i++)
{
Console.WriteLine(one[i]);
}
Console.ReadLine();
}
}
}
Array.Sort
is a static function and returns void. So both tries were almost right, but not completely. Check it here: http://msdn.microsoft.com/en-us/library/6tf1f0bc.aspx
I read this page of $$anonymous$$SDN many times, but I don't no how can I put this code in Unity. I tried your code, but the program returns this: The name `Array' does not exist in the current context
$$anonymous$$an, I'm sorry!! I thought that System; it was the same thing that System.Collections; of unity. Now everything works fine!! Thank you very much! :D
Answer by ckfinite · Jun 26, 2011 at 06:40 PM
Well, what imports do you have? You need to get System.Linq in order to use IEnumerable.Sort()
Edit: After some research, try using IEnumerable.OrderBy instead.
playerScore.OrderBy(score=>score);
Hi, macfanpro I'm newbie, and I don't understand how I can use IEnumerable.Sort(), but I go to research. Thanks for your answer!
Just copy in the line I edited with. For ease, use this:
playerScore.OrderBy(score=>score);
Array.Sort
also works fine, but you can't assign it to a second variable.
Oh, I made a mistake. OrderBy doesn't change the source array, it returns an enumerable that is sorted. Try this:
int[] toSort = new int[] {35, 53, 32, 27, 16, 72, 44, 83, 51, 91};
foreach (int sort in toSort.OrderBy(sorted=>sorted))
{
Debug.Log(sort);
}