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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by Binxalot · Mar 27, 2014 at 07:33 PM · javascriptlistssort

Javascript List, Sort a floating point number? [Solved]

So I've found a bug in my code which I didn't see at first, but it's there and I need to fix it. I have a racing game and I want to sort the scores by Minutes, Seconds, Milliseconds. Scores are saved as floats and then converted to a string that shows MM:SS:MMM

I save all the scores in to a list, which I then try to sort using this function:

 var HighScores = new List.<float>();

 function sortScore(a : float, b : float){
   return a - b;
 }

This works perfectly fine, until a score is very close to another score, the sort function doesn't sort anything after the decimal in the floating point number. The scores get screwed up if they're too close together. For example something like this first place 20.001 second place: 30.003 third place 40.033 will sort properly.

But something like this: First place 30.001 Second Place 30.384 third place 31.988 will screw up the sorting, the larger of the two scores, 30.384 will be first, 30.001 will be second, and 31.988 will show as 3rd.

I read that JavaScript will treat numbers as strings when sorting. A suggestion was to build a custom sort function which I did; Here's how I sort the list:

  HighScores.Sort(sortScore());

Is there any way to prevent this from happening? My only work around would be to make the predefined scores something like this 31.999 32.999 etc. I'd like to just find a fix for this without having to resort to that, it would look unnatural.


Okay so I solved this on my own, probably could have waited to post this, but I think others might run in to this same issue, lots of developers create racing games so here's the fix:

 function sortScore(a : float, b : float){
   return a - b;
 }

becomes

 function sortScore(a : float, b : float){
   return (a * 10000) - (b * 10000);
 }

it now sorts properly.

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
1

Answer by JOHNMCLAY · Aug 23, 2014 at 12:09 AM

Hey Binxalot,

Just stopping by to show my utmost appreciation for posting this. I was in the same hair-pulling, teeth-grindingly frustrating situation until 5 minutes ago...and now, thanks to your simply elegant solution, my teeth remain seated and my hair intact! :D

Thanks a lot! :D

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 kendrakelly · Feb 02, 2019 at 09:19 PM

@Binxalot Also just a quick thanks for following up with the solution!! Not working on a game but trying to sort businesses by distances from a current location... this worked wonderfully.

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

22 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

Related Questions

How do I create and loop through a completely generic list in javascript? 1 Answer

merge sort crash Unity 2 Answers

Help with sorting values from a class 2 Answers

Orderby/Sort a List of Lists based on the .Count 1 Answer

Sort() javascript arrays 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