• 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
0
Question by gotanidea · Jun 26, 2012 at 01:06 PM · javascriptarraycomparelengthsort

Sort Multiple Arrays By Their Length

I have multiple arrays:

http://jsfiddle.net/NgcjG/

I need to sort the arrays by their length. Eg:

http://jsfiddle.net/k8WAk/

How to find out which one is the largest, the second largest, the third largest, and the least array?

Comment
Add comment
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

1 Reply

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

Answer by whydoidoit · Jun 26, 2012 at 01:09 PM

You can do it like this:

 import System.Linq;
 import System.Collections.Generic;

 var arrays = new List.<Array>();
 arrays.Add(arr_a);
 arrays.Add(arr_b);
 arrays.Add(arr_c);
 arrays.Add(arr_d);
 var sortedArrays = arrays.OrderBy(function(a) { return a.length; }).ToList();
 var shortestArray = sortedArrays[0];
 var nextShortest = sortedArrays[1];

I have to say using Array is not a good idea in Unity Script - there are a lot of better collection classes (like the ones I've used here). You should use a strongly typed collection where possible or a List implementation.

Comment
Add comment · Show 5 · 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 gotanidea · Jun 27, 2012 at 04:58 AM 0
Share

Thank you !

avatar image gotanidea · Jun 27, 2012 at 06:39 AM 0
Share

I'm sorry, after trying this code, I got errors:

"ArgumentOutOfRangeException: Argument is out of range. Parameter name: index"

When I tried to print the "nextShortest" variable, I got:

"Null UnityEngine.$$anonymous$$onoBehaviour:print(Object)"

I attached the Javascript code to the $$anonymous$$ain Camera. I only added "print(nextShortest);" line:

http://jsfiddle.net/DvDPY/

What went wrong?

avatar image whydoidoit · Jun 27, 2012 at 12:15 PM 0
Share

Did you put it inside a function? You didn't in your jsfiddle example...

  function Start() {
     var arrays = new List.<Array>();
    ....
  }
avatar image gotanidea · Jul 02, 2012 at 05:44 AM 0
Share

Hey, it works! Thank you very much!!

avatar image whydoidoit · Jul 02, 2012 at 08:41 AM 0
Share

No problem. Just remember Unity Script really isn't Javascript. It just looks a bit like it... (and unfortunately is officially called JavaScript which really doesn't help)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Set variable/Array to a length by scripting 2 Answers

Help with sorting values from a class 2 Answers

Sorting a list by distance to an object? 1 Answer

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges