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 rmail2006 · May 09, 2011 at 05:10 PM · arrayarraysburgzergarcadehackandslash

Sort Two Arrays

Hi everyone,

I've started the Hack and Slash tutorial on BurgzergArcade. It is shown in c# but i've been using javascript and I now need to sort an array based on another.

One array is a list of GameObjects and the other is a list of float variables, which are the distance between each gameobject and the character.

What I need to do is sort the distances in ascending order and also apply the same sort to the gameobject.

For example: Both arrays start like this:

Array1 = [Enemy1, Enemy2, Enemy3];
Array2 = [20, 10, 30];

And when sorted should end up like this:

Array1 = [Enemy2, Enemy1, Enemy3];
Array2 = [10, 20, 30];

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
3
Best Answer

Answer by almo · May 09, 2011 at 06:01 PM

I'd use a bubble sort, since it's easy to program. Unity has built-in sort, but only a basic one that sorts the items in the array with regard to each other.

PS: This code is untested, so it may not just work out of the box. But I hope it gets across the idea.

var foundone : bool; var tempobj : GameObject; var tempfloat : float;

foundone = true; while(foundone) { foundone = false; for(i = 0; i < Array2.length - 1; i++) { if(Array2[i] > Array2[i + 1]) { tempobj = Array1[i + 1]; tempfloat = Array2[i + 1]; Array1[i + 1] = Array1[i]; Array2[i + 1] = Array2[i]; Array1[i] = tempobj; Array2[i] = tempfloat; foundone = true; } } }

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 rmail2006 · May 09, 2011 at 08:20 PM 0
Share

This worked perfectly Thanks :D

avatar image almo · May 09, 2011 at 08:22 PM 0
Share

Sure, no problem. :)

avatar image QasimS · Dec 26, 2018 at 09:34 PM 0
Share

@almo Can I say that I have just spent the last 2 hours laboring over how to get this working. I tried every goddamn bubble sort algorithm I knew to man, but because my knowledge is mostly python it just all screwed up. You absolutely saved my life here man, thank you so much for this. I know this was posted a very long time ago for you, but I still need to thank you for this regardless.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

my QandA array is not working when you choose 3 wrong answers 1 Answer

Array empties values on RunTime? 1 Answer

Array with two values per element 1 Answer

Having Multiple Controllable FPS Units Selected From A Singular RTS Mode? 0 Answers

Array Within Index 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