Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
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.

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

Array error - Index is less than 0... 3 Answers

Sorting array 0 Answers

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

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

Sorting a list by distance to an object? 1 Answer


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