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 /
This question was closed May 03, 2012 at 05:51 AM by hijinxbassist for the following reason:

Question answered in form of comment by Eric5h5

avatar image
0
Question by hijinxbassist · May 03, 2012 at 12:34 AM · arraycomparesortbuiltin

Sorting array

This should be an easy one, but im having a lil trouble. I have a builtin array of transforms. I have all the names ascending from 1 and up. Im not sure how to sort them by name, tried a custom function of comparing 2 transforms by name and using that in the Sort() function with no luck. I tried a C# version that i was unable to use bcz there was an error and im not to sharp in C#. Im coding unity-java. Can someone help me out, provide some insight and an example. Thanks

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 syclamoth · May 03, 2012 at 01:24 AM 0
Share

Basically, switch to C# (because it's in general a more useful language) and then use generic Lists. They have a 'Sort' function built in.

avatar image hijinxbassist · May 03, 2012 at 01:41 AM 0
Share

@syclamoth Can i use generic lists in unity java? Im guessing no, since you said to make the switch to C#.

I was hoping that i could do this using the Array() Sort function. What i have so far doesnt work...

 function ChildArray()
 {
     var array=new Array();
     for(var s:int=0;s<transform.childCount;s++)
     {
     array.push(transform.GetChild(s).transform);
     }
     array.Sort(Sorter);
 }
 function Sorter(A:Transform,B:Transform):int
 {
     return A.name.CompareTo(B.name);
 }
avatar image Eric5h5 · May 03, 2012 at 04:01 AM 0
Share

Unity doesn't use Java, but you can use Lists in Unityscript/Javascript; there's no reason to switch to C#, it's not in general more useful. People who say that ins$$anonymous$$d of answering the question have...issues. Also there's no reason to use Array, in fact you should stay away from it. If you replace Array with List.<Transform> in that code (and push with Add), it will work. You can also use Linq, though I'm not a fan of its rather obscure syntax.

 array = array.OrderBy(function(a){return a.name;}).ToList();
avatar image hijinxbassist · May 03, 2012 at 05:50 AM 0
Share

@Eric5h5 Good deal. I gave it a shot earlier but didnt see there was a period before part of it. Youre right the other method is quite obscure looking.

Thanks for the 2 methods, i ended up writing a brute force method

 function SortArray(array:Transform[]):Transform[]
 {
 var sortedArray:Transform[]=new Transform[array.length];
 for(var i:int=0;i<array.length;i++)
 {
     for(var x:int=0;x<array.length;x++)
     {
     if(array[i].name==(x+1).ToString())
     {
         sortedArray[x]=array[i];
         break;
     }
     }
 }
 return sortedArray;
 }

But this is soooo limited and dependent on sequential numbering. Im gonna switch over to the list method, sounds much more efficient and i can save some lines. Thanks!

0 Replies

  • Sort: 

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

Sort Multiple Arrays By Their Length 1 Answer

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

Sorting a list by distance to an object? 1 Answer

List.Sort with IComparer 2 Answers

Problems adding to a Builtin Array in C# 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