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 Dave 3 · Jul 19, 2010 at 09:27 PM · javascriptarraysorting

Sort only considering last entry in my array

I'm filling up this array with these custom IComparable objects and then trying to sort them. My problem is that when it does the sort it's only considering the last entry in the array. This custom class stuff is a little over my head so any help will be greatly appreciated.

import System;

class PriorityEntry extends System.IComparable { var gD : float; var tD : float; var xC : float; var yC : float; var fR : float; var sR : float;

 function CompareTo(other : System.Object) : int
 {
     if (!(other instanceof PriorityEntry)) return;
     var peOther : PriorityEntry = other;
     if( gD != peOther.gD ) return gD.CompareTo(peOther.gD);
     if( tD != peOther.tD ) return tD.CompareTo(peOther.tD);
     if( fR != peOther.fR ) return fR.CompareTo(peOther.fR);
     if( sR != peOther.sR ) return sR.CompareTo(peOther.fR);
 }

}

sorry, the code formatting is not cooperating with me for some reason.

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

1 Reply

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

Answer by qJake · Jul 19, 2010 at 09:52 PM

Wow... there are lots of potential problems here.

First and foremost, read the MSDN documentation on CompareTo:

http://msdn.microsoft.com/en-us/library/system.icomparable.compareto(VS.71).aspx

Now... the potential problems I see are:

if (!(other instanceof PriorityEntry))

I don't know how UnityScript works, but that might have to be "is" instead of "instanceOf".

if (!(other instanceof PriorityEntry)) return;

ICompare.CompareTo always returns an int. Throw an exception if you can't return an int.

A lot of this I'm not sure about. For .NET-intensive things like this (implementing interfaces, overriding methods, type-checking, etc), you really should be using C#, instead of the poorly-implemented UnityScript. This is what your script would look like in C#:

using System;

public class PriorityEntry : IComparable { float gD; float tD; float xC; float yC; float fR; float sR;

 public int CompareTo(Object other)
 {
     if (!(other is PriorityEntry))
     {
          throw new Exception("Cannot compare!");
     }

     PriorityEntry peOther = other as PriorityEntry;

     if (gD != peOther.gD) return gD.CompareTo(peOther.gD);
     if (tD != peOther.tD) return tD.CompareTo(peOther.tD);
     if (fR != peOther.fR) return fR.CompareTo(peOther.fR);
     if (sR != peOther.sR) return sR.CompareTo(peOther.fR);

     // You need to return something here, too, or use an if/else structure.
 }

}

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 Dave 3 · Jul 19, 2010 at 11:00 PM 0
Share

The problem that I'm encountering doing it this way is that when I try to refer to this class in another script (var priorityCollector : PriorityEntry;) I get the error that 'PriorityEntry' does not denote a valid type.

avatar image qJake · Jul 20, 2010 at 12:08 AM 0
Share

Stick the script in the Plugins/ folder, it will compile first.

avatar image Dave 3 · Jul 20, 2010 at 03:57 AM 0
Share

ah, ok. Sorry to keep co$$anonymous$$g back with follow-up questions (you're being really helpful) but in the script where I'm actually referring to this class and I define a variable of the type 'PriorityEntry' (var priorityCollector : PriorityEntry;) I get the error that this object reference is not set to an instance of this object, and that just makes me scratch my head. This second script is written in java.

avatar image qJake · Jul 20, 2010 at 04:56 AM 0
Share

UnityScript, not "java" (it's actually javascript, not java, they're completely different things). And you need to create a new instance of your class. I don't know how with UnityScript, but something like var priorityCollector = new PriorityEntry();

avatar image Dave 3 · Jul 20, 2010 at 06:54 PM 0
Share

ah, thanks for clarifying.

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

No one has followed this question yet.

Related Questions

Sorting by multiple variables 3 Answers

I'm desperate with my scoreboard! (Array, Javascript) 1 Answer

Sorting an array of vectors in javascript 1 Answer

For Loop Array Problem 1 Answer

Index out of bounds even tho it shouldnt be [Javascript] 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