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 /
avatar image
0
Question by LoubieLou96 · Mar 22, 2019 at 05:33 PM · sortingsortracing gameranking

Order an array of classes using 2 variables (Racing game, ranking system)

I'm trying to create a ranking system for a racing game, I have an array of AI cars and a separate variable for the players car. They are stored using 'Car' class which keeps track of the individual cars lap count and waypoint count (integers) - it also holds the car ID so I know which car is which.

I then combine all the cars (AI and Player) into one array called 'racePositions', I now want to sort that array first by the lap count (highest to lowest) and then look at the waypoint count to see which car is ahead of the other if they're on the same lap count.

E.g. The unsorted version of 'racePositions' might look like: [0] car: 1, currLap = 4, currWaypoint = 12 [1] car: 2, currLap = 4, currWaypoint = 2 [2] car: 3, currLap = 2, currWaypoint = 12 [3] car: 4, currLap = 6, currWaypoint = 5

But the sorted array would look like: [0] car: 4, currLap = 6, currWaypoint = 5 [1] car: 1, currLap = 4, currWaypoint = 12 [2] car: 2, currLap = 4, currWaypoint = 2 [3] car: 3, currLap = 2, currWaypoint = 12

I'm not sure how to do the sort and I've been looking at it for so long I think I've got the coders version of writers block. I feel like the answer is staring me in the face but I just can't see it haha, any help would be much appreciated :-) 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

1 Reply

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

Answer by Bunny83 · Mar 22, 2019 at 06:22 PM

The easiest way is to create a linearised sorting value. The lap count is the major sorting value so the current waypoint doesn't matter if the lap count differs. Only when the lap count is the same you need to differentiate by the waypoint.


A common way is to simply muliply the lap count by the number of waypoints and add the current waypoint index (assuming counting starts at 0). This way you get a continuous value For example if you have 24 waypoints it would look like this:

 // unsorted
 [0] car: 1, currLap = 4, currWaypoint = 12, position = 108 (==4*24 + 12)
 [1] car: 2, currLap = 4, currWaypoint = 2,  position =  98 (==4*24 + 2)
 [2] car: 3, currLap = 2, currWaypoint = 12, position =  60 (==2*24 + 12)
 [3] car: 4, currLap = 6, currWaypoint = 5,  position = 149 (==6*24 + 5)
 
 // sorted
 [0] car: 4, currLap = 6, currWaypoint = 5,  position = 149 (==6*24 + 5)
 [1] car: 1, currLap = 4, currWaypoint = 12, position = 108 (==4*24 + 12)
 [2] car: 2, currLap = 4, currWaypoint = 2,  position =  98 (==4*24 + 2)
 [3] car: 3, currLap = 2, currWaypoint = 12, position =  60 (==2*24 + 12)


Instead of the number of waypoints you could also use an arbitrary multiplier as long as it's large enough (100 or 1000 for example). You can simply sort the list based on the "position". This position can be calculated on the fly. For example if you have a List<Car> cars you can simply do:

 cars.Sort((a,b)=>(a.lap*1000+a.currentWayoint).CompareTo(b.lap*1000+b.currentWayoint));

A bit cleaner would be to implement a property / getter for the position in your car class and / or let your Car class implement the IComparable interface.

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 LoubieLou96 · Mar 25, 2019 at 05:18 PM 0
Share

Thanks for your answer, I've gone with a slight variation of your second recommendation. For anyone who wants to know, after a lot of searching I came across this answer too: https://answers.unity.com/questions/913636/ranking-system-in-racing-game.html

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

102 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 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 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

Sort a generic list by member 1 Answer

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

How do you sort a dictionary by value? 2 Answers

Illustrating Sorting Process of Balls In Unity Using Sorting Algorithms 0 Answers

sorting list of key, value by value 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