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 15, 2010 at 08:14 PM · javascriptarraysorting

Sorting by multiple variables

I've figured out how to create a custom class in Javascript that extends IComparable and allows me to sort an array filled with these by a single variable, but my problem is that I need this class to sort by 3 different numbers, rather than just one.

So, let's call my class PriorityEntry, and every instance of this class has a set of a, b, c, d, and e variables. I want to populate an array with instances of PriorityEntry and then sort them by the a variable. Whenever a is equal between the two (which will happen a lot) I want to then compare between the b variables, which if they are similar (which will happen sometimes) then I want to finally compare the c variables (which are randomly determined numbers, to avoid similarity. If these turn out to be similar on occasion I could probably add a fourth variable to sort by which is also randomly determined.)

I just don't get how you can do a more complicated sort than the single-variable comparisons, and if anybody can help me in this area I'd be incredibly grateful.

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
1
Best Answer

Answer by burnumd · Jul 15, 2010 at 08:33 PM

Here's a .NET forum user's similar problem. Essentially, in your CompareTo, do whatever sort you want on a but don't return it yet. If they're equal, do your secondary/tertiary/quaternary sort as necessary.

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
1
Best Answer

Answer by skovacs1 · Jul 16, 2010 at 03:01 PM

It's very simple as burnumd said. If one of these answers solved your problem, please mark it as the accepted answer.

If you were looking some code (too lazy to translate C# to js?) or couldn't understand burnumd's linked response, it will look something like this (if you are using the IComparable interface and not the Comparable implementation (which wouldn't have to check the argument type)):

Unity mono

public int CompareTo(object other)
{
    if (!(other is PriorityEntry))
    {
        throw new ArgumentException("object is not a PriorityEntry");
    }
    PriorityEntry PEOther = other as PriorityEntry;
    if( a != PEOther.a ) return a.CompareTo(PEOther.a);
    if( b != PEOther.b ) return b.CompareTo(PEOther.b);
    if( c != PEOther.c ) return c.CompareTo(PEOther.c);
    if( d != PEOther.d ) return d.CompareTo(PEOther.d);
    return e.CompareTo(PEOther.e);
}

Unity js

function CompareTo(other : System.Object) : int
{
    if (!(other instanceof PriorityEntry)) return;
    var PEOther : PriorityEntry = other;
    if( a != PEOther.a ) return a.CompareTo(PEOther.a);
    if( b != PEOther.b ) return b.CompareTo(PEOther.b);
    if( c != PEOther.c ) return c.CompareTo(PEOther.c);
    if( d != PEOther.d ) return d.CompareTo(PEOther.d);
    return e.CompareTo(PEOther.e);
}
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 Dave 3 · Jul 17, 2010 at 02:43 AM 0
Share

Thanks skovacs, sorry for leaving the question up for a while. $$anonymous$$y internet access has been dodgy so I haven't had an opportunity to check up on it until now.

avatar image
0

Answer by raul corrales · Mar 24, 2011 at 05:23 PM

This works if you are interest it http://www.youtube.com/watch?v=3560eDQ8shw

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

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

Sort only considering last entry in my array 1 Answer

Sorting an array of vectors in javascript 1 Answer

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

Sorting an Transforms array by gameobject name 1 Answer

working with array index... 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