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
0
Question by POLYGAMe · Apr 06, 2013 at 12:38 PM · arrayarrayssortingsort

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

Okay, it's late and I'm tired and it's probably something REALLY simple but I'm trying to sort this array. Basically, I want to set the split times in order and I can't see why the following code isn't working, but it's not...

   function SortSplitTimes()
     {
         for (var i = 0; i < (AIDriverArray.length - 1); i++)
         {
             for (var j = (i + 1); j < AIDriverArray.length; j++)
             {
                 if (AIDriverArray[i].fSplitTime < AIDriverArray[j].fSplitTime)
                 {
                     fTemp = AIDriverArray[i].fSplitTime;
                     AIDriverArray[i].fSplitTime = AIDriverArray[j].fSplitTime;
                     AIDriverArray[j].fSplitTime = fTemp;
                  } 
             }
             
             print(AIDriverArray[i].fSplitTime);
         }
     }

Help greatly appreciated!

Comment
Add comment · Show 14
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 whydoidoit · Apr 06, 2013 at 12:41 PM 0
Share

Just a point - didn't you fancy using .Sort on the array?

   Array.Sort(AIDriverArray, function(a,b) a.fSplitTime < b.fSplitTime ? -1 : 1);
avatar image whydoidoit · Apr 06, 2013 at 12:44 PM 1
Share

The problem with your existing code is that it's changing the split time for the drivers (not ordering the drivers themselves!!!)

avatar image POLYGAMe · Apr 06, 2013 at 12:44 PM 0
Share

I'm not using the Array class... will that matter? I guess I could give it a go. Nearly 2am... TI$$anonymous$$E FOR $$anonymous$$ORE COFFEE!!!!

avatar image whydoidoit · Apr 06, 2013 at 12:45 PM 1
Share

Not if AIDriverArray is an array - if it's a list then you can do:

     AIDriverArray.Sort(function(a,b) a.fSplitTime < b.fSplitTime);
avatar image Cygon4 · Apr 06, 2013 at 09:07 PM 1
Share

If you manage to get the sorting function called, the issue @whydoidoit's pointed out still hits you. Your method doesn't sort the AIDrivers, only their times (example: if Schumacher was faster than Alonso, he isn't listed before him, ins$$anonymous$$d, Alonso gets Schumacher's time and Schumacher gets Alonso's time :))

Show more comments

1 Reply

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

Answer by POLYGAMe · Apr 07, 2013 at 03:29 AM

Okay, answered... shows how tired I was. I'd created the array of Driver cars but I hadn't populated it. HAHAHAHAHA. WHat an egg... I was up for hours, too...

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

13 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

Related Questions

Need recommendation for a sorting method for Enemy distances from Player (Ascending) - C# 1 Answer

How to create a table, array of array? 1 Answer

my QandA array is not working when you choose 3 wrong answers 1 Answer

Array empties values on RunTime? 1 Answer

Array with two values per 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