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
1
Question by SrBilyon · Jan 10, 2013 at 01:39 PM · arrayenumsortingcompare

Comparing value of multiple ints and returning the largest.

I'm going to have a series many integers, and after a while, I want to get all their values, and return the largest int in the series, then perform an action based on the actual variable returned (not its value).

Ex. Suppose we took a bunch of ints that represented foods, and after comparing the amount of each food present,apple has the most and is returned as 'apple' (or maybe its index if using an array), not the amount of apples.

I was thinking of using an array and an enum, but I'm not sure of the easiest and least redundant approach to doing this.

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

3 Replies

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

Answer by Wolfram · Jan 10, 2013 at 07:53 PM

Today is your lucky day, I was in the mood to provide a script ;-)

 using UnityEngine;
 using System.Collections;
 
 public class FruitClass : MonoBehaviour {
     
     public enum Fruits {
         Apple,
         Orange,
         Banana,
         numEntries // we use this as marker for the number of entries
     }
     
     public int[] myFruits;
     
     void Awake(){
         // provide necessary space
         myFruits=new int[(int)Fruits.numEntries];

         // fill array
         myFruits[(int)Fruits.Apple]=3;
         myFruits[(int)Fruits.Orange]=1;
         myFruits[(int)Fruits.Banana]=4;

         // find index of maximum value
         int maxIndex=-1;
         int maxValue=0;
         for(int i=0;i<(int)Fruits.numEntries;i++){
             if(myFruits[i]>maxValue){
                 maxValue=myFruits[i];
                 maxIndex=i;
             }
         }

         // output result
         if(maxIndex>=0)
             Debug.Log("The fruit I have most of are "+(Fruits)maxIndex+"s, of which there are "+maxValue+".");
     }
 }

The int<->Fruits casts cannot be prevented with this approach in C#, it forces a compiler error when missing.

EDIT: Look! I even documented it! :-D

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 SrBilyon · Jan 10, 2013 at 10:36 PM 0
Share

Thank you so much. I'm going to try this as soon as I get in front of my workstation!

avatar image
2

Answer by $$anonymous$$ · Jan 10, 2013 at 11:06 PM

Or you can use System.Collections.Generic.Dictionary, where your keys are from the Fruit enum, the values are integers.

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

For getting the min/max items, you can use LINQ, like described here:

http://stackoverflow.com/questions/3653970/minimum-value-in-dictionary-using-linq

This is a simple and elegant solution.

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 Landern · Jan 10, 2013 at 01:44 PM

The easiest way of doing it using a standard array(square brackets).

 // c#
 
 int[] nums = { 1, 2, 3, 4, 5, 6, 7 };
 
 nums.Max(); // Will result in 7
 nums.Min(); // Will result in 1
 
 // US/JS
 
 var nums: int[] = { 1, 2, 3, 4, 5, 6, 7 };
 
 nums.Max(); // Will result in 7
 nums.Min(); // Will result in 1
 
Comment
Add comment · Show 5 · 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 SrBilyon · Jan 10, 2013 at 01:54 PM 0
Share

Ah, so in the declaration of int[] nums, could I do something like int[] nums = { apple = 3, orange = 1, banana = 4} and expect to have the $$anonymous$$ be orange and the max be banana? Would I need to declare the fruit outside of the int[] declaration? Lastly, is $$anonymous$$ and max returning the index or the actual largest value?

avatar image Landern · Jan 10, 2013 at 04:26 PM 0
Share

you could not, in this case i would create a custom class(i'm not sure if you are using c# or unity/javascript) that used an enum type for say Fruit property and a property of type int that was i assume the Amount.

It is returning the max or $$anonymous$$ because this is an int type, it will find the highest or lowest value in the array and return a single int.

avatar image SrBilyon · Jan 10, 2013 at 07:31 PM 0
Share

Ah, okay. I happen to be using C#

avatar image Wolfram · Jan 10, 2013 at 11:33 PM 0
Share

@z_murc: I think that comment deserves to be posted as a separate answer.

With the drawback in $$anonymous$$d you always have when using Linq: it's very elegant and can accomplish a lot with only one or two lines of code - but if you need performance, you need to be careful and know exactly what's going on, otherwise it can become very expensive very quickly.

avatar image $$anonymous$$ · Jan 11, 2013 at 10:32 AM 0
Share

@Wolfram : converted it. You can put this comment under it. Indeed you have to be careful with it, but in this case (some fruits and their amounts) I don't tink it poses an issue.

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

10 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

Related Questions

Comparing Two Arrays 1 Answer

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

Sorting array 0 Answers

What to use to store data like eg. block types? 1 Answer

List Sorting But Random Order 2 Answers


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