x


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' takes0' 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!!

more ▼

asked Jun 26 '11 at 06:37 PM

zlabs gravatar image

zlabs
1 1 1 2

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

Jun 26 '11 at 08:38 PM Thom Denick

List is the current recommended List data structure. However, if you have a REALLY (1Mb+) 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.

Jun 26 '11 at 08:57 PM ckfinite

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.

Jun 26 '11 at 09:23 PM Eric5h5

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.

Jun 26 '11 at 09:34 PM ckfinite
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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

more ▼

answered Jun 26 '11 at 06:51 PM

Marnix gravatar image

Marnix
1.3k 22 31 45

I read this page of MSDN 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

Jun 26 '11 at 07:15 PM zlabs

Have you seen: using System;? VERY important sentence.

Jun 26 '11 at 07:19 PM Marnix

Man, 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

Jun 26 '11 at 07:34 PM zlabs
(comments are locked)
10|3000 characters needed characters left

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);
more ▼

answered Jun 26 '11 at 06:40 PM

ckfinite gravatar image

ckfinite
1.3k 5 9 24

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!

Jun 26 '11 at 06:55 PM zlabs

Just copy in the line I edited with. For ease, use this:

playerScore.OrderBy(score=>score);
Jun 26 '11 at 07:01 PM ckfinite

Array.Sort also works fine, but you can't assign it to a second variable.

Jun 26 '11 at 07:03 PM Marnix

You did add the line using System.Linq, right?

Jun 26 '11 at 07:39 PM ckfinite

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);
}
Jun 26 '11 at 08:15 PM ckfinite
(comments are locked)
10|3000 characters needed characters left

hey just use System namespace it works

more ▼

answered May 18 '12 at 11:40 AM

Abhi gravatar image

Abhi
0

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1396
x280
x61
x34
x5

asked: Jun 26 '11 at 06:37 PM

Seen: 10079 times

Last Updated: May 18 '12 at 11:40 AM