• 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
Question by bpears · Oct 28, 2013 at 12:39 AM · listfloatintconditionalgeneric list

Get the two highest floats in a genericList without using .Sort()?

Hello, I am trying to get two highest floats in list without Sort(). Why on earth would omseone want to do that? Well, I need to maintain the order they entered the list, so I know which number pertains to which object. And since the number is a dynamic variable I cannot simply trace it back to the original gameobject. The gameObject is being used in a equation, in which I put that number into a list.

Now Im trying to figure out how to do this. I thought I had it figured out, and coded out the possibility manually, and it kind of works, and kind of doesn't. It's strange, maybe using too many layers of conditional math inside single if statements. But Im hoping maybe Im overlooking an easier way.

The highest value is easy enough, but to get the two highest is... a little more complicated. Since you have to compare to previous numbers and depends if the highest was first, then you cant simply check for the next highest as, " is higher than the previous", basically. So yeah.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by robertbu · Oct 28, 2013 at 01:08 AM

I don't see any slick way but brute force:

 void TwoHighest(out float highest, out float second, List<float> floats) {
     highest = Mathf.NegativeInfinity;
     second = Mathf.NegativeInfinity;
     for (int i = 0; i < floats.Count; i++) {
         if (floats[i] >= highest) {
             second = highest;
             highest = floats[i];
         }
         else if (floats[i] > second) {
             second = floats[i];    
         }
     }
 }

This is C#. You'd have to fiddle more to get two values out of a function in Javascript.

Comment
bpears

People who like this

1 Show 4 · 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 bpears · Oct 28, 2013 at 06:45 AM 0
Share

I tried something like that, but it just wasnt working out for me. So, I did it the hard way, and it kind of works but it seems to not know how to compute at this level of, embedded-ness, I guess.

 if(((float0>float1)||(float1>float0))&&(float1>float2)&&(float1>float3)){



I have several of those, but they dont seem to be working right. I have debugged all floats and they are correct. Maybe Ill have to get even more down to the core... sigh.

avatar image bpears · Oct 28, 2013 at 07:03 AM 0
Share

im basically saying," I dont care which value is the highest out of the two highest, but if both of those, are indeed higher than the others, do something specific for that scenario". There are 4 floats, and 6 different versions of that code. but it just doesnt seem to be working correctly.

avatar image bpears · Oct 28, 2013 at 07:08 AM 0
Share

Hmmm.. I just debugged that code with just plain numbers and it computed fine. Strange.

avatar image bpears · Oct 28, 2013 at 07:26 AM 0
Share

turns out my code was perfect! I had 2 extra scenarios of outcome that in this case I didn't need, because they didn't apply to the use. Hard to explain without writing a book but basically I should have only had 4 varieties of my code in this situation. However your answer to this specific question is valid! so, correct! lol

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

A node in a childnode? 1 Answer

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

Can I create a list with an int/float and a string? C# 2 Answers

How to i set the int to hitpoints / 10 + the current int. 1 Answer

Why is this Int lagging when its subtracting something over a period of time? 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