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
5
Question by chief1234 · Jul 13, 2010 at 10:10 PM · arraysortbuiltin array

Sorting builtin arrays

I need to sort the return of RaycastAll (a builtin array) from the nearest object to the farthest, but I can't figure out how to sort builtin arrays in a nice, tidy fashion.

What would be the best way to accomplish this?

Thanks

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

2 Replies

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

Answer by Mike 3 · Jul 13, 2010 at 10:14 PM

http://msdn.microsoft.com/en-us/library/system.array.sort.aspx

You'd have to write your own comparer class, but it's pretty trivial (and very customizable)

Edit:

import System;

function Start () { var raycasthits = Physics.RaycastAll(Camera.main.transform.position, Camera.main.transform.forward);

 var output = ""; //this is for debugging
 for(var rch in raycasthits) output += rch.collider.name + "   "; //this is for debugging
 Debug.Log(output); //this is for debugging

 System.Array.Sort(raycasthits, new RaycastSorter());

 output = ""; //this is for debugging
 for(var rch in raycasthits) output += rch.collider.name + "   "; //this is for debugging
 Debug.Log(output); //this is for debugging

}

class RaycastSorter implements IComparer //class RaycastSorter extends IComparer //in unity 2.6 { function Compare(a : System.Object, b : System.Object) : int { if ( !(a instanceof RaycastHit) || !(b instanceof RaycastHit)) return; var raycastHitA : RaycastHit = a; var raycastHitB : RaycastHit = b;

     return sqDistA.distance.CompareTo(sqDistB.distance);
 }

}

Comment
Add comment · Show 5 · 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 chief1234 · Jul 13, 2010 at 11:35 PM 0
Share

Wow - couldn't ask for a better answer. Thanks - you've saved me a ton of time and many headaches!

avatar image chief1234 · Jul 14, 2010 at 12:03 AM 0
Share

Update: I found that the square magnitude calculation was a little unreliable, so I'm using raycastHitA.distance ins$$anonymous$$d.
Not sure why it works better, but it does.

avatar image Mike 3 · Jul 14, 2010 at 08:05 AM 0
Share

Ah, right - my original tests were with FindObjectsOfType(Collider), so I had to grab the distance from Collider objects ins$$anonymous$$d of raycasthit ones - using distance is a lot less code indeed, so changed the example for future readers, thanks :)

avatar image DaveA · Jan 29, 2011 at 01:22 AM 0
Share

I think you need 'implements' rather than 'extends'

avatar image Mike 3 · Jan 29, 2011 at 04:14 AM 0
Share

Does now - the question was for 2.6 though heh

avatar image
10

Answer by skovacs1 · Jul 13, 2010 at 11:06 PM

This looks like this post. You could write your own comparer class or function or just store an IComparable class within your array.

Using an IComparable

Unity mono

using System.Collections.Generic;

public class RaycastResult: IComparable<RaycastResult> { public float distance; public Collider collider;

 public RaycastResult(RaycastHit hit)
 {
     distance = hit.distance;
     collider = hit.collider;
 }

 public int CompareTo(RaycastResult other)
 {
     return distance.CompareTo(other.distance);
 }

}

//A sorted collection SortedSet<RaycastResult> raycastResults = new SortedSet<RaycastResult>();

void SortDistances() { raycastResults.Clear(); RaycastHit[] hits = Physics.RaycastAll(transform.position, transform.forward, 100.0);

 foreach(RaycastHit hit in hits)
 {
     raycastResults.Add(new RaycastResult(hit));
 }
 //With an unsorted collection, call raycastResults.sort();

}

Unity js

import System;

class RaycastResult implements IComparable { var distance : float; var collider : Collider;

 function RaycastResult(hit : RaycastHit)
 {
     distance = hit.distance;
     collider = hit.collider;
 }

 function CompareTo(other : System.Object) : int
 {
     if (!(other instanceof RaycastResult)) return;
     var raycastResultOther : RaycastResult = other;
     return distance.CompareTo(raycastResultOther);
 }

}

var raycastResults : Array = new Array();

function SortDistances() { raycastResults = new Array(); var hits : RaycastHit[] = Physics.RaycastAll(transform.position, transform.forward, 100.0);

 for(var hit : RaycastHit in hits)
 {
     raycastResults.push(new RaycastResult(hit));
 }
 raycastResults.sort();

}

Using Comparer

Unity mono

public class RaycastComp : Comparer<RaycastHit> { public int Compare(RaycastHit x, RaycastHit y) { return x.distance.CompareTo(y.distance); } }

RaycastHit[] hits = Physics.RaycastAll(transform.position, transform.forward, 100.0); RaycastComp rc = new RaycastComp();

//...Somewhere else put Array.Sort(hits, rc);

Unity js (pretty much what Mike posted)

class RaycastComp extends IComparer { function Compare(x : System.Object, y : System.Object) : int { if ( !(x instanceof RaycastHit) || !(y instanceof RaycastHit)) return; var raycastHitX : RaycastHit = x; var raycastHitY : RaycastHit = x; return raycastHitX .distance.CompareTo(raycastHitY.distance); } }

var hits : RaycastHit[] = Physics.RaycastAll(transform.position, transform.forward, 100.0); var rc : RaycastComp = new RaycastComp();

//...Somewhere else put System.Array.Sort(hits, rc);

Using a comparison function

Unity mono

public int CompareHits(RaycastHit x, RaycastHit y) { return x.distance.CompareTo(y.distance); }

RaycastHit[] hits = Physics.RaycastAll(transform.position, transform.forward, 100.0);

//...Somewhere else put Array.Sort(hits, CompareHits);

Unity js

function CompareHits(var x : RaycastHit, var y : RaycastHit) : int { return x.distance.CompareTo(y.distance); }

var hits : RaycastHit[] = Physics.RaycastAll(transform.position, transform.forward, 100.0);

//...Somewhere else put hits.Sort(CompareHits);

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 chief1234 · Jul 13, 2010 at 11:34 PM 0
Share

Thanks - this will be helpful in the future

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

No one has followed this question yet.

Related Questions

Find gameObject with higher variable 1 Answer

Trying to sort an array. What's wrong here? 1 Answer

Dynamic Turn Order and Display Based On Initiative Value (Javascript)? 2 Answers

How can I order an array of RaycastHits in reverse order of distance? 1 Answer

Ordering an array of GO based on interfaced variables using linq. 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