• 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
Question by NewDeveloper · Sep 25, 2012 at 01:55 PM · listclasssortingby

Orderrring a List Ascending or Descending C# Advanced

Hey guys I am wanting to create an array with class this is my class

 [System.Serializable]
 public class Objects{
 public String Name;
 public float Value
 }

and i have this variable

 public Objects[] ObjectsList;

Now I need two function that orders the list

 public enum order {ascending, descending}
 public enum by {string, float}
 public void Order (order Order, by By) {
 // this function has to order ascending or descending by string or float.
 // string = Name
 // float = value
 }

How can i do this? I have seen Array.Sort, but I am not sure, Can anyone help me please? Thanks see you, byeeee!

Comment

People who like this

0 Show 0
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

Answer by Julien-Lynge · Sep 25, 2012 at 05:34 PM

This really isn't anything Unity specific - if you want to know how to sort a list in C#, try googling it. There are many posts out there, for instance this one which appeared first in google for me:

http://www.dotnetperls.com/sort-list

Comment
Fattie
shaderop
Bunny83

People who like this

3 Show 3 · 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 NewDeveloper · Sep 26, 2012 at 12:37 PM 0
Share

Thanks, but this is not really what i am looking for, i have 2 variables, and i need to sort the list by one.

avatar image shaderop · Sep 26, 2012 at 06:42 PM 1
Share

The URL Julien posted has most of the information you need. Look at the LINQ example in particular. If that doesn't help then google for LINQ and go from there.

And sorry for nit-picking, but "Objects" is not a good name for a class.

avatar image NewDeveloper · Sep 26, 2012 at 07:01 PM 0
Share

It' s oke, I will study them. Yeah you are right, Objects is not a good name for class it continue to confuse with Object, (System.Object)... See you byeee!

avatar image

Answer by jacksparrow · Sep 25, 2012 at 07:11 PM

You can use a delegate to specify how to sort. Like this:

 Things[] thingArray = new Things[3];
 public enum order {ascending, descending};
 public enum byvar {bystring, byfloat};

     thingArray[0] = new Things("betty",1);
     thingArray[1] = new Things("catherine",2);
     thingArray[2] = new Things("abel",3);
     void mysort(order Order, byvar By) {
         if (Order == order.ascending) {
             if (By == byvar.bystring) {
                 System.Array.Sort(thingArray, delegate(Things thing1, Things thing2) {
                     return thing1.tname.CompareTo(thing2.tname);
                 });
             }
             else if (By == byvar.byfloat) {            
                 System.Array.Sort(thingArray, delegate(Things thing1, Things thing2) {
                     return thing1.number.CompareTo(thing2.number);
                 });
             }
         }
         else {
             if (By == byvar.bystring) {
                 System.Array.Sort(thingArray, delegate(Things thing1, Things thing2) {
                     return thing2.tname.CompareTo(thing1.tname);
                 });
             }
             else if (By == byvar.byfloat) {            
                 System.Array.Sort(thingArray, delegate(Things thing1, Things thing2) {
                     return thing2.number.CompareTo(thing1.number);
                 });
             }
         }
     }
Comment
Bunny83

People who like this

1 Show 3 · 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 NewDeveloper · Sep 26, 2012 at 12:40 PM 0
Share

Thanks, this is looking good, but can you explain this? because I don't know what to put on delegate (Things thing1, Things thing2). Thank you so much for the script, i think hat this might help me, if i can resolve the problem of Things thingX

avatar image jacksparrow · Sep 26, 2012 at 06:30 PM 0
Share

Just use your Objects (and the appropriate variable names 'Name' and 'value') in place of the 'Things' with 'tname' and 'number'.

avatar image NewDeveloper · Sep 26, 2012 at 07:03 PM 0
Share

So you suggest me: delegate (Objects name, Objects value) right? but what is thing1.tname -> name.tname????

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

A node in a childnode? 1 Answer

javascript, default object comparator 1 Answer

How to store a class as a variable 1 Answer

Why Is TextMesh Off by One ??? 0 Answers

Integer List values don't get initialized in Constructor of Class 2 Answers


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