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 Rush3fan · Mar 30, 2012 at 10:15 PM · arraycustomsortingfloats

Custom sorting function - need help

Hi, I'm trying to sort an array of intergers that looks somewhat like this:

 [-650,-750,-820,-960,-460,-660,-990,0,0,0,0]

This is my sorting function:

 function compare(a,b){
 return a-b;
 }

I want it to put the highest number on top, but the zeros on the bottom of the array. Does anyone know how I can get it to do that?

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

2 Replies

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

Answer by Rabbit-Stew-dio · Mar 31, 2012 at 09:58 AM

Use a smarter Compare function:

 function compare(a: int,b: int)
 {
   if (a == b) return 0;
   if (a == 0) return +1;
   if (b == 0) return -1;
   // otherwise compare normally
   return a-b;
 }

And then sort the list. You will have to use the list from the System.Collection.Generic namespace. You cannot use the Unity-Array class, as it lacks a customizable sort operation.

 List<int> yourlist;
 yourlist.Sort(Compare);
 

  

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 Rush3fan · Mar 31, 2012 at 03:30 PM 0
Share

I've never tried a list-array (or whatever you call it), but I'll give it a try. Thanks!

avatar image Rush3fan · Mar 31, 2012 at 03:31 PM 0
Share

I'll mark it correct with thumbs up if it does work. :)

avatar image Rush3fan · Apr 02, 2012 at 03:45 AM 0
Share

Hey, umm.. I hate to say, but it didn't work. I use javascript and it's complaining about the + sign. Wow.. I was just writing tons of code with no problems until I hit this. This sorting thing is confusing.

avatar image Rush3fan · Apr 02, 2012 at 04:00 AM 0
Share

Still working with it though.. I took out that plus sign and the syntax errors went away.. I still can't make heads or tails on it though. Lots of debugging at this stage and have no idea how to use the list functions. :( Doesn't mean I can't learn..

avatar image Rush3fan · Apr 02, 2012 at 04:54 PM 0
Share

Wow! Works excellent! Does exactly what I wanted. Thank you!

avatar image
0

Answer by keld-oelykke · Mar 30, 2012 at 10:59 PM

 List<int> list = new List();
 // add numbers
 list.Sort(); // sorts ascending - this is what you need I guess
 list.Reverse(); // now descending

Cheers, Keld

Comment
Add comment · Show 2 · 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 Rush3fan · Mar 31, 2012 at 12:44 AM 0
Share

But that would leave the zeros at the top. I just want the zeros to stick at the bottom regardless of their value.

avatar image keld-oelykke · Mar 31, 2012 at 04:56 PM 1
Share

ok... wasn't sure how you wanted it sorted. You then also need the custom sort function as $$anonymous$$ suggests.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Sort only considering last entry in my array 1 Answer

Sorting an Array of GameObjects by Name 1 Answer

Comparing Two Arrays 1 Answer

Working with Arrays for Custom Inspector 0 Answers

Creating A Basic Path For Enemies 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