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 /
  • Help Room /
avatar image
0
Question by Griffo · Feb 26, 2016 at 09:09 AM · listlistsactivesortingsort

Sort list by distance AND by being active

Hi,

I have a list of game objects that I sort by distance.

 _enemyList.Sort(ByDistance);
 
 function ByDistance(a: GameObject, b: GameObject) : int
 {
     var dstToA = Vector3.Distance(_player.transform.position, a.transform.position);
     var dstToB = Vector3.Distance(_player.transform.position, b.transform.position);
     return dstToA.CompareTo(dstToB);
 }

But what I want to do is to sort them by distance and only if they are active.

Do I then need to call again?

 _enemyList.Sort(ByActive);
 
 function ByActive(a: GameObject, b: GameObject) : int
 {
     var activeA = a.activeSelf;
     var activeB = b.activeSelf;
     return activeA.CompareTo(activeB);
 }

?

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

3 Replies

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

Answer by Owen-Reynolds · Feb 26, 2016 at 03:31 PM

You can run a sort twice in a row, and it will work as a double-sort, but not in C# (well, not using C# Sort.) There's a thread on StackOverFlow mocking C# for this.

This is an actual ComSci topic. A Stable sort keeps the order of things that are tied. It's specifically for what you're describing. Sorting by distance, then again by Active, would work if Sort was stable. All the Actives would go to the front, without breaking the sort by distance. But an unstable sort just scrambles everything during each one.

But, you could just write one function to do both:

 // all actives to front, regardless if distance:
 if(activeA != activeB) return CompareTo(activeA, activeB);
 // else both have same active/inactive, use distance:
 return CompareTo(distA, distB);
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
avatar image
1

Answer by phil_me_up · Feb 26, 2016 at 01:29 PM

You'll need to implement your own comparison system, and the easiest way is to probably use the IComparable or IComparer interfaces:

https://support.microsoft.com/en-us/kb/320727

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
avatar image
0

Answer by Griffo · Feb 27, 2016 at 06:13 AM

@phil_me_up thanks for the link.

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

36 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How I Sort a List at Start?,Sorting a List on Start 0 Answers

How can I sort a list of GameObject by name? 1 Answer

Generate unique random numbers and sort them. 3 Answers

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index 2 Answers

List give a count of zero when it have 1 element 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