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 RDooM · Aug 13, 2014 at 02:31 AM · listdelegatesort

Sort list with non-anonymous delegate?

I never like anonymous functions. So instead of using an anonymous delegate :

 Targets.Sort(delegate(Transform x, Transform y) {
             return Vector3.Distance(x.position, myTransform.position).CompareTo(Vector3.Distance(y.position, myTransform.position));
         });

Can't you just use a something like this :

 delegate int Comp(Transform t1, Transform t2);
 
     int Comparare(Transform t1, Transform t2){
     
         float d1, d2;
 
         d1 = Vector3.Distance (t1.position, myTransform.position);
         d2 = Vector3.Distance (t2.position, myTransform.position);
     
 
         return d1.CompareTo (d2);
     }
 
 
 then where you need the sorting you make an instance of the delegate and pass it to the Sort: 
 
 Comp compari = Comparare;
 Targets.Sort(compari); ?


Clearly something is missing, but is it even possible? What is wrong?

Comment
Add comment · Show 1
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 _dns_ · Aug 13, 2014 at 06:45 PM 0
Share

Hi, nothing to do with the sorting itself: you could also use Vector3.sqr$$anonymous$$agnitude to save some computations. http://docs.unity3d.com/ScriptReference/Vector3-sqr$$anonymous$$agnitude.html

2 Replies

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

Answer by vexe · Aug 13, 2014 at 06:39 PM

List.Sort does not take a compare delegate. It takes an IComparer object to use to determine how to compare your objects.

So in your case you could create a TransformComparer, like so (not tested, but should work ok):

 class TransformComparer : IComparer<Transform>
 {
     public Transform myTransform;

     public int Compare(Transform x, Transform y)
     {
         return Vector3.Distance(x.position, myTransform.position).CompareTo(Vector3.Distance(y.position, myTransform.position));
     }
 }

And then:

 var comparer = new TransformComparer { myTransform = myTransform };
 targets.Sort(comparer);

Also, I don't get what you don't like about anonymous methods... You don't have to use the old C# delegate syntax that way, you could use lambda expressions (you don't even need the curely braces nor the return statement if you had only one statement...)

So you could sort your targets in LINQ like so:

 var orderdTargets = targets.OrderBy(t => Vector3.Distance(t.transform.position, myTransform.position));

Isn't that sweet?

(See JK's videos on lambdas if you're not so familiar with them)

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 ian_sorbello · Aug 14, 2014 at 01:28 AM 0
Share

very nice :)

avatar image
0

Answer by ian_sorbello · Aug 13, 2014 at 03:26 AM

Look like you are on the right track. When you ask what is wrong, what exactly is not working?

You might need to declare your compare function as static.

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 RDooM · Aug 13, 2014 at 06:22 PM 0
Share

Well, basically the Target.Sort does not recognise the "compari" instance of the delegate as a valid parameter.

"Argument #1' cannot convert Targeting.Comp' expression to type `System.Collections.Generic.IComparer'"

I tried making the compare function static, but that opens a whole other can of worms. Like "An object reference is required to access non-static member"

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Sorting a list by distance to an object? 1 Answer

A node in a childnode? 1 Answer

Show Top 10 Of 1 GameType 2 Answers

Sorting a list of GameObjects by accessing their int values 2 Answers

Sorting Game Object Name In Numerical and Alphabetical Order via List 2 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