• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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 can I sort a list of GameObject by name? 1 Answer

Generate unique random numbers and sort them. 3 Answers

Problem with Lists and Remove 0 Answers

Inventory slots number are wrong 0 Answers

Creating card game - copy of list to new list - creating cycle 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges