• 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 Howey-Do-It · May 02, 2013 at 11:14 AM · arraylistvectorracingsort

How to Sort Multiple Vector Arrays in order of Values

My goal was to find a way to sort a Vector3 based on the value of X, then the value of Y, and then Z. Therefore if value1.x is less than value2.x check the y values.

The application for this code was to keep track of the current place (1st, 2nd, etc.) of race cars on a track using 3 variables: Laps, Specific Points, and Distance to the next Point. If you are a Lap ahead then it would ignore the next point and distance to that point.

After hours of exhaustive research I found the code I needed that I adapted to sort multiple vectors. I decided to post it here because I could not find this specific information anywhere, related to sorting Vector2's, 3's, etc.

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

1 Reply

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

Answer by Howey-Do-It · May 02, 2013 at 11:14 AM

using System;

public class SortingVectors : MonoBehaviour {

     public Vector3[] playerInfo;

     void Start () 
     {
         playerInfo = new Vector3[8];
     }
  
 
     void Update () 
     {
      Array.Sort(playerInfo, Vector3Compare);    
     }


     private int Vector3Compare(Vector3 value1, Vector3 value2)
     {
          if (value1.x < value2.x)
         {
             return -1;
         }
         else if(value1.x == value2.x)
         {
             if(value1.y < value2.y)
             {
                 return -1;
             }
             else if(value1.y == value2.y)
             {
                 if(value1.z < value2.z)
                 {
                     return -1;
                 }
                 else if(value1.z == value2.z)
                 {
                     return 0;
                 }
                 else
                 {
                     return 1;
                 }
             }
             else
             {
                 return 1;
             }
         }
         else
         {
             return 1;
         }
     }

}

This arranges the Vector3's based on the X value first, Y second, and then Z.

While this may be very straightforward to many of you, it wasn't until I wrote this out that I fully understood what declaring an int/function actually allows you to do (I probably still don't fully understand).

I hope this helps someone and I believe people will find other applications for this, even with Vector2's or Vector4's.

Thanks for being awesome!

God bless!

Howey

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

12 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

Related Questions

Sorting Variables Help 1 Answer

[C#] Sorting a List of Gameobjects Alphabetically 2 Answers

What is the quickest way of getting the most common item in a list or array? 1 Answer

Sorting a list by distance to an object? 1 Answer

Show Top 10 Of 1 GameType 2 Answers

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