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 Rush3fan · Mar 16, 2012 at 09:47 PM · arrayfloatintsort

How do I sort floats by highest to lowest value?

Say I have an array filled with times, but in seconds accurate down to the millisecond. How would I go about sorting these in order from highest to lowest value?

I have tried this method out: http://www.zparacha.com/sort_numeric_arrays/#.T2KysRFuksI

But, it only seems to sort intergers. I need it to sort down to the decimal.

Comment
Add comment · Show 2
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 DaveA · Mar 17, 2012 at 12:18 AM 0
Share

Did you try that code you linked to? I don't think it would care if it were ints or floats

avatar image Rush3fan · Mar 17, 2012 at 12:37 AM 0
Share

Yea.. it worked, but, for some reason, it rounded everything up. One thing I tried doing is multiplying by 10000 so that the decimal places would be counted for, but it's really a pain to do it that way.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by rutter · Mar 16, 2012 at 10:16 PM

If you're not worried about the internals of the sort, letting the .NET framework take care of it for you may save some headaches. You didn't mention which particular data structure you're using, so I'll offer an example which includes some popular choices.

 #pragma strict
 
 import System.Collections.Generic;
 
 //sort a built-in array of floats
 var x = new float[3];
 x[0] = 300.1;
 x[1] = 200.2;
 x[2] = 500.3;
 System.Array.Sort(x);
 
 //sort an Array() of floats
 var y = new Array();
 y.Push( 300.1 );
 y.Push( 200.2 );
 y.Push( 500.3 );
 y.Sort();
 
 //sort a .NET list of floats
 var z = new List.<float>();
 z.Add( 300.1 );
 z.Add( 200.2 );
 z.Add( 500.3 );
 z.Sort();
 
 //reverse our lists
 System.Array.Reverse(x);
 y.Reverse();
 z.Reverse();
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 Rush3fan · Mar 16, 2012 at 11:58 PM 0
Share

Whats a data structure?? I'm a javascript guy, so I'm a little confused.. $$anonymous$$y array already contains the floats. I just have to put them in a different order. I'm not seeing what you are doing with 300.1 or 200.2, because those should just already be in the array to start with. I have no idea what .Net, pragma strict, or import System Collections Generic even means. :/

It's just a simple array - 8 floats that are completely out of order. I am trying to judge a timed race where 8 cars each finish with a different time. The question is: who took first place, second place, third place.. etc. The times have to be put in order, and then I can say: if(myTime==finishedTime[1])me="first place";

See what I mean now? It shouldn't be so complicated. I just can't figure out why that example I linked to above rounds my floats up to the nearest whole number.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Does genericList.Sort() go low to high? or high to low? - for ints/floats 1 Answer

Find Highest Value in Array 5 Answers

Monodevelop is expecting int from my float array 3 Answers

"Talent Tree" global yes/false var and int? 3 Answers

Random.Range Problems (Errors CS1502 and CS0266) 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