Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Finalfire · Sep 22, 2011 at 07:22 PM · javascriptarrayssort

Sort() javascript arrays

I have a javascript that holds some items for my game. The items all have a script attached, and variables that determine how many there are and what type of item it is.

I was wondering if it was possible to use Sort() to arrange the items in the array so that I could sort it by type, and then by the object's name, both are contained within the script.

I've looked at the documentation for sort, and it's not very helpful.

http://unity3d.com/support/documentation/ScriptReference/Array.Sort.html

searching on the internet, I've been able to find some hints that what I want might be possible, but nothing that is really usable.

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
2

Answer by jonas.du · Sep 22, 2011 at 07:56 PM

If you use generic lists instead of unity arrays you have a lot more possibilities. There is a List.Sort (Generic Comparison) method where you can provied a custom comparison.

Read the part "Generic Lists" in this blog post (very helpful!).
Here the full documentation for Generic Lists.

I'm not sure if you can use the extension methos in unity but if, you could solve your problem as mentioned in this question.

Comment
Add comment · Show 4 · 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 Finalfire · Sep 22, 2011 at 10:03 PM 0
Share

Alright this seems like it might work, can you use it in javascript? I have a very long script that I'm using and to rewrite it in C# would be less than ideal.

avatar image jonas.du · Sep 23, 2011 at 09:39 AM 0
Share

This works in javascript too, check out the blog post in the answer.

 var myList = new List.<Type>(); 
avatar image Finalfire · Sep 27, 2011 at 03:41 PM 0
Share

When I try and change my array to a generic list it says that

"List does not devote a valid type."

Is this a pro feature, or do I have to do something besides just declare it?

avatar image Finalfire · Sep 27, 2011 at 04:18 PM 0
Share

Also, I found this on while looking up more on Javascript Arrays

http://www.javascriptkit.com/javatutors/arraysort2.shtml

however to sort strings, you need to compare them, unity does not allow this.

"$$anonymous$$issing $$anonymous$$ethod Extension String.opLess than" (or something along those lines)

Is there a way to make this work?

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 array does not update when object is destroyed. How do I fix it? (java) 2 Answers

how to save an array into an object 1 Answer

Javascript Multidimensional Arrays 0 Answers

JesseEtzler's RPG Talent System in C# 1 Answer

Send a message to multiple scripts within an Array - Javascript 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