Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by zlabs · Jun 26, 2011 at 06:37 PM · arrayscoresortcs0103

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Thom Denick · Jun 26, 2011 at 08:38 PM 0
Share

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

avatar image ckfinite · Jun 26, 2011 at 08:57 PM 0
Share

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.

avatar image Eric5h5 · Jun 26, 2011 at 09:23 PM 0
Share

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.

avatar image ckfinite · Jun 26, 2011 at 09:34 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zlabs · Jun 26, 2011 at 07:15 PM 0
Share

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

avatar image Marnix · Jun 26, 2011 at 07:19 PM 2
Share

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

avatar image zlabs · Jun 26, 2011 at 07:34 PM 0
Share

$$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

avatar image
2

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);
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zlabs · Jun 26, 2011 at 06:55 PM 0
Share

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!

avatar image ckfinite · Jun 26, 2011 at 07:01 PM 0
Share

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

 playerScore.OrderBy(score=>score);
avatar image Marnix · Jun 26, 2011 at 07:03 PM 0
Share

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

avatar image ckfinite · Jun 26, 2011 at 07:39 PM 1
Share

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

avatar image ckfinite · Jun 26, 2011 at 08:15 PM 2
Share

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);
 }
Show more comments
avatar image
0

Answer by Abhi · May 18, 2012 at 11:40 AM

hey just use System namespace it works

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it necessary to create an Array() to show high scores in PlayerPref? 1 Answer

Trying to get Vector3 Array to sort in order of Y values - C sharp 1 Answer

How do I sort floats by highest to lowest value? 1 Answer

Matching Index of two Arrays after one Array is sort 3 Answers

[C#] Sorting a List of Gameobjects Alphabetically 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges