• 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 sparkzbarca · Nov 21, 2012 at 02:40 AM · sort

how sort works?

So I looked at the documentation and its of no help.

I looked at a few of the possible related questions and didnt really see a good explanation.

Basically what I want is

I'm using C#

I have an ArrayList (should I change to List? i'm unclear on whats considered best, its kind of odd actually how ambigious this seems Array, List, ArrayList, its odd for such an established and successful game engine. :P)

the array list is an array of vector3

i would like to sort by the Y Component only (actually at some point each component but I can infer the code for X and Z from Y)

some kind of

array[3].y > array[2].y > array[1].y

or the reverse.

I promise I don't normally ask for a code handout its just that the documentation doesnt seem to be there for this. I'm thinking it should be just one line

Comment
Add comment · Show 2
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 Eric5h5 · Nov 21, 2012 at 04:08 AM 0
Share

The documentation is at $$anonymous$$SDN, which explains it all. You should not use ArrayList. It's not odd, it's just that ArrayList is old. The difference between arrays and List is pretty clear. http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

avatar image sparkzbarca · Nov 21, 2012 at 04:15 AM 0
Share

could you look at my attempt to create a compare function to feed sort and see if its right?

its a comment under the last answer

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Loius · Nov 21, 2012 at 07:31 AM

I actually JUST wrote that exact functionality, so good luck is yours:

     private var orderComparer : IComparer = new DrawOrderComparer();
     function get sortedAUVs() : ArrayList { sortableAUVs.Sort(orderComparer); return sortableAUVs; }
     private var sortableAUVs : ArrayList;

 class DrawOrderComparer implements IComparer {
     static var t1: AnimatedUV;
     static var t2: AnimatedUV;
     
     function Compare( a : Object, b : Object ) : int {
         auv1 = a as Transform;
         auv2 = b as Transform;
         
         if ( auv1.position.y > auv2.position.y ) return 1;
         if ( auv1.position.y < auv2.position.y ) return -1;
         return 0;
     }
 }

As for Array/ArrayList/List:

Never use Array.

List is great; a little slower than builtin but lots of helper functions for resizing it or adding/removing specific elements, and significantly more manageable than Array.

[] array ("builtin" array) is always fastest, but its size isn't dynamic - if you need to change how many elements it contains, you need to rebuild it from scratch.

I never use ArrayList, so I don't know much about it. Apparently it has a nice overrideable Sort function. x) It looks like List has a sort function just like ArrayList - I'd recommend using that (Dangit now I've gotta go back in and change up my sorter :))

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 sparkzbarca · Nov 21, 2012 at 07:47 AM 0
Share

YAY! thanx

avatar image
0

Answer by nameless323 · Nov 21, 2012 at 03:25 AM

c# doesn't konw how compare elements try to impement IComparer interface. this will help http://msdn.microsoft.com/en-us/library/system.collections.icomparer.aspx

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 nameless323 · Nov 21, 2012 at 03:27 AM 0
Share

and use List in System.Collections.Generic http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx it safety for a types

avatar image
0

Answer by aldonaletto · Nov 21, 2012 at 03:32 AM

The fastest option is builtin arrays, like this:

 Vector3[] points = new Vector3[100];

You can sort them with Array methods like Array.Sort, where you can define the comparison function.
But these arrays have fixed sizes - if you want to dynamically add or remove elements, List is the best option (don't use ArrayList, it's way slower). You can use List methods to sort the array as well, like List.Sort.
The Unity docs only cover the Unity engine - google for "whatever you want" + ".Net" to find info about the .Net/Mono specific functions (the Unity docs should give us this hint!)

Comment
Add comment · Show 2 · 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 sparkzbarca · Nov 21, 2012 at 03:58 AM 0
Share

im still vague on this is this right roughly to make my own sort that sorts by the y value an array of vector3

         public Vector3 Compare(Vector3 first, Vector3 second)
         {
         if( first.y >= second.y)
            return first;
         else 
           return second;
         }
     
     mylist.Sort(Compare);
avatar image sparkzbarca · Nov 21, 2012 at 06:55 AM 0
Share

I think thats the right way to basically override the default sort mechanism but i'm not sure.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to import the object from server to unity 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 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