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
1
Question by Flynn · May 04, 2012 at 11:27 PM · arraysort

Unity JS array custom sort

Hello :) I've been all over Unity answers tyring to answer this one myself, but nothing I come across appears to be working...

The following is what I'd like to do:

 var arr = [1, 2, 3, 4, 6, 5];
 arr.Sort(sortFunc);
 
 function sortFunc(a, b)
 {
 return b-a;
 }
 Debug.Log(arr);//[6, 5, 4, 3, 2, 1]

However, the actual syntax for applying a custom sort function to a JS generic array is proving difficult to find. Anyone willing to offer some help? (P.S. this is for Unity 2, NOT 3 (compatability reasons, hard to explain))

EDIT 1:

 The array I am sorting is constructed like so:
 
 var arr = ["a", "b", "c", "d"];
 
 //Next, I run arr through a function that returns:
 
 var arr = [["a", 1], ["b", 2], ["c", 3], ["d", 4]];
 
 //Finally, try to sort it as shown above.

My sorter is arranged like so:

 function SortIEByType(a : Array, b : Array)
 {
     var ra = parseInt(a[0][1]);
     var rb = parseInt(b[0][1]);
     return ra-rb;
 }

I have specified the a and b as Array as you did for integers... No effect :( I also had a : int appended to my method, but that did not work either.

Any ideas?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · May 05, 2012 at 12:30 AM

Well, I got it in 3.5 - don't know if this works in 2.x

var arr = new Array(1, 2, 3, 4, 6, 5); arr.Sort(sortFunc);

function sortFunc(a:int, b:int){ // I had to declare the types return b-a; }

Debug.Log(arr);//[6, 5, 4, 3, 2, 1]

EDITED: For custom types, you should create a class:

class Element { // declare a class for the array elements: var name:String; var num:int;

function Element(name:String, num:int){ this.name = name; this.num = num; } }

var arr = new Array(new Element("a",1), new Element("b",2), new Element("c",3), new Element("d",4));

arr.Sort(sortFunc);

function sortFunc(a:Element, b:Element){ return b.num-a.num; }

for (var el:Element in arr) Debug.Log(el.name+" "+el.num); Your second function could receive an array of Element and assign their num field, then you could call Sort.
NOTE: You must declare arr = new Array(), or a built-in array will be created instead - and built-in arrays have no Sort method, nor are resizable.

Comment
Add comment · Show 1 · 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 Flynn · May 05, 2012 at 03:16 AM 0
Share

See post edits above:

avatar image
0

Answer by ricardHagerman · May 26, 2014 at 05:54 PM

pragma strict

var segments : GameObject[]; private var words : int;

function Start () {

bullets = 0;

// finds all gameObjects with the tag bullet

segments = GameObject.FindGameObjectsWithTag("bullet");

// if all gameObjects with the tag "bullet" is named "0", "1", "2", .... then this will sort the gameObjects in asending oreder, provided that the number of gameObjects with the tag bullet is 10. (you could use segments.lenght)

for(var i = 0; i < 10 - 1; i++){ for(var j = 10 - 1; j > i; j--){ var temp; if( parseInt(segments[j].name) < parseInt(segments[j - 1].name)){ temp = segments[j]; segments[j] = segments[j - 1]; segments[j - 1] = temp; } } } }

function Update () {

//Starts an animation for one bullet (starting with bullet 0) when the "s" key is presed by activating an transition in the bullets animator witht the trigger "bom". The same trigger is used for all bullets.

if(bullets < 10){

if(Input.GetKeyDown("s")){

Debug.Log(segments[worbullets].gameObject.name);

segments[bullets].gameObject.GetComponent(Animator).SetTrigger("bom ");

words ++;

}

}

}

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

6 People are following this question.

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

Related Questions

Help with sorting values from a class 2 Answers

List.Sort with IComparer 2 Answers

Sort array of GameObjects for a Building System. 1 Answer

Storing a variable and it's name 3 Answers

sort gui button 0 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