Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by ProjectCryken · Apr 27, 2014 at 04:35 PM · arrayclasses

Sort an array of classes by a variable value.

This is my class:

 public class RareBlock

     {
         public string name;
         public Vector3 blocks;
         public int chance;
     }

I have an array of many instances of this class. How do I sort the array according to the value of chance?

Thanks

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 KellyThomas · Apr 27, 2014 at 04:56 PM 0
Share

How often will you be perfor$$anonymous$$g the sort? Is it time critical? Will the items be randomly distributed or partially ordered?

Any of these factors could influence the optimal solution.

Bubble Sort is an easy to understand and implement sort algorithm, or you can use LINQ's OrderBy() as a ready made solution.

4 Replies

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

Answer by joshpsawyer · Apr 27, 2014 at 04:50 PM

Use the Array.Sort() method that takes a Comparison<T>:

 Array.Sort(myRareBlockArray,
 delegate(RareBlock x, RareBlock y) { return x.chance.CompareTo(y.chance); });
Comment
Add comment · Show 12 · 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 joshpsawyer · Apr 27, 2014 at 05:03 PM 1
Share

Also, if you know that you'll always want to sort the RareBlock class in this way, you might implement the IComparable<t> interface... it will simplify your calls, at least.

avatar image KellyThomas · Apr 27, 2014 at 05:35 PM 1
Share

Are you using "`Array.Sort()`"? Sort is a static method of the Array type.

avatar image KellyThomas · Apr 27, 2014 at 05:49 PM 1
Share

Please post the line of code causing the error

avatar image KellyThomas · Apr 27, 2014 at 06:02 PM 1
Share

Please change the first line to:

 Array.Sort(sortedBlockTypes, 
 
avatar image KellyThomas · Apr 27, 2014 at 06:18 PM 1
Share

Ok, then you need using System; at the top of your file.

Show more comments
avatar image
1

Answer by Cassos · Apr 28, 2020 at 05:39 PM

If anyone needs this in 2020, I created a function for a recent project, sorting and returning an array of custom classes, by first making it into a dictionary:

     public Dialogue[] SortDialogues(Dialogue[] _ds)
     {
         Dictionary<string, Dialogue> _dic = new Dictionary<string, Dialogue>();

         for (int i = 0; i < _ds.Length; i++)
             _dic.Add(_ds[i].id + i, _ds[i]);

         List<string> keys = new List<string>();
         foreach (KeyValuePair<string, Dialogue> _d in _dic)
             keys.Add(_d.Key);
         keys.Sort();

         List<Dialogue> ds = new List<Dialogue>();
         foreach (string s in keys)
                 ds.Add(_dic[s]);

         return ds.ToArray();
     }

Have fun with it! ^^

Greetings, Max

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

Answer by Acegikmo · Apr 28, 2020 at 06:22 PM

Linq version, if you want that!

 myArray = myArray.OrderBy( x => x.chance ).ToArray();
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 Aykutkaraca · Jan 15 at 03:50 PM 0
Share

Good one! thanks. And I figured out if you want the order descending:

 myArray = myArray.OrderByDescending( x => x.chance ).ToArray();

avatar image
0

Answer by Adamcbrz · Apr 27, 2014 at 04:49 PM

Here is the link to the documentation for having a compare method with array.sort. From there you can tell it what how to compare.

http://msdn.microsoft.com/en-us/library/cxt053xf(v=vs.110).aspx

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

28 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 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

How to use Javascript Array with classes? 1 Answer

Need my function to work with different lists of different values (classes) 1 Answer

Instantiating objects from a class? (C#) 1 Answer

Accessing variable from a class 1 Answer

Array of custom class objects all return the same value? 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