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 Rockgol · Apr 17, 2013 at 04:08 AM · javascriptarrayclasssort

Help with sorting values from a class

Well, i need to do show some labels with bot's points in javascript, so I have a class Bot.

 #pragma strict
 
 class Bot extends MonoBehaviour
 {
     var name = "";
     var animal = "";
     var pointsScene1 : float = 0;
     var pointsScene2 : float = 0;
     var pointsScene3 : float = 0;
     var totalPoints : float = 0;
     
     function get Name() : String { return name; }
     function set Name(value : String) { name = value; }
     
     function get Animal() : String { return animal; }
     function set Animal(value : String) { animal = value; }
     
     function get PointsScene1() : float { return pointsScene1; }
     function set PointsScene1(value : float) { pointsScene1 = value; }
     
     function get PointsScene2() : float { return pointsScene2; }
     function set PointsScene2(value : float) { pointsScene2 = value; }
     
     function get PointsScene3() : float { return pointsScene3; }
     function set PointsScene3(value : float) { pointsScene3 = value; }
     
     function get TotalPoints() : float { return totalPoints; }
     function set TotalPoints(value : float) { totalPoints = value; }
 }
 

This class is populate with random values, until here is all perfect. But i need to "sort" this class to find out the the best bot with "TotalPoints" and complete the labels!

I assign they in Inspector using

 var arr : Bot[];

Any ideas how can i do this?

I tryed something like this to find out

 function Top8(arrTmp : Bot[]) {
     var max = arrTmp[0].TotalPoints;
     for (i = 1; i < arrTmp.Length; i++) {
        if (arrTmp[i].TotalPoints > max) {
          max = arrTmp[i].TotalPoints;
        }
     }
     return max;
 }

I called this function using

 Top8(arr);

And I got the highest value TotalPoints, but I need to check from what bot is, and show it in GUI.Label to do a top 8 including 7 bots and player's points

Im accepting any sugestions!

Comment
Add comment · Show 1
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 codecranker · Apr 17, 2013 at 05:01 AM 0
Share

Ins$$anonymous$$d of returning 'max' why not return 'arrTmp[i]' (the entire bot object)? You will have all the properties for that bot.

2 Replies

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

Answer by Eric5h5 · Apr 17, 2013 at 04:52 AM

Make a function to compare the totalPoints values, either inline:

 var botsArray : Bot[];
 
 function Start () {
     System.Array.Sort (botsArray, function(a : Bot, b : Bot) a.totalPoints.CompareTo(b.totalPoints));
 }

Or as a separate function:

 var botsArray : Bot[];
 
 function Start () {
     System.Array.Sort (botsArray, CompareTotalPoints);
 }
 
 function CompareTotalPoints (a : Bot, b : Bot) : int {
     return a.totalPoints.CompareTo(b.totalPoints);
 }

Some observations: does Bot extend MonoBehaviour for a reason? You have the variables in the Bot class as public, so the getters/setters (which aren't doing anything) seem redundant.

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 Rockgol · Apr 17, 2013 at 05:21 PM 0
Share

Thanks Eric5h5, this compate function worked!

$$anonymous$$y Bot class extends $$anonymous$$onoBehavior because I need to assign the bot array in inspector, without this extend I cant do this.

avatar image
-1

Answer by The-IT664 · Apr 17, 2013 at 04:27 AM

I would suggest you take a look at a simple sorting algorithm like bubble sort (http://en.wikipedia.org/wiki/Bubble_sort). It's easy to understand and implement, and although it is quite inefficient as sorting algorithms go (n^2), your set is so small it won't make a meaningful difference.

Also, just a suggestion on your current implementation. Instead of storing the max TotalPoints, store and return the Bot instance itself and call Bot.TotalPoints to get the max value.

Edit: While you're at it, I highly recommend spending a bit of time learning about different sorting algorithms. They are vital in many different aspects of programming.

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 Eric5h5 · Apr 17, 2013 at 04:52 AM 0
Share

There's usually no real reason to make your own sorting algorithm when .NET/$$anonymous$$ono has sorting built in.

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

14 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

Related Questions

Static array with custom class? 1 Answer

Sort Multiple Arrays By Their Length 1 Answer

Creating A Class Variable That Works With Any Class 0 Answers

Unity JS array custom sort 2 Answers

Why Does Initialising Built-In Array of Classes Require Constructor? 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