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 Calvin_And_Hobbies · Jan 27, 2015 at 08:52 PM · c#listsorting

Sorting a list by three parameters (C#)

I have this list called "Block". Each item has four parts being a string called "block", and three floats for each coordinate:

 public class Block : IComparable<Block>
 {
     public string block;
     public float xLocation;
     public float yLocation;
     public float zLocation;
     
     public Block (string newBlock, float newXLocation, float newYLocation, float newZLocation)
     {
         block = newBlock;
         xLocation = newXLocation;
         yLocation = newYLocation;
         zLocation = newZLocation;    
     }
 }

I want to order the blocks first by lowest x value, then lowest y value, and finally lowest z value.

So if I had this in the list:

  • "Stone", -20, 30, 15

  • "Slope", -20, 20, 50

  • "Stone", 40, 5, 0

  • "Slope", -20, 20, 10

It would end up like this:

  • "Slope", -20, 20, 10

  • "Slope", -20, 20, 50

  • "Stone", -20, 30, 15

  • "Stone", 40, 5, 0

Try to keep in mind I'm still not that good at Unity so if you can, try to provide some explanation on what the code is doing. Also I prefer C#.

Thanks in advance!

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 LessThanEpic · Jan 27, 2015 at 09:58 PM

Your block class will need to have a CompareTo method. It would probably look something like this:

 public int CompareTo(object otherObject)
 {
     Block otherBlock = (Block) otherObject;
     
     if(this.xLocation < otherBlock.xLocation)
     {
         return -1;
     }
     else if(this.xLocation > otherBlock.xLocation)
     {
         return 1;
     }
     
     //the x matched, so lets check y
     if(this.yLocation < otherBlock.yLocation)
     {
         return -1;
     }
     else if(this.yLocation > otherBlock.yLocation)
     {
         return 1;
     }
     
     //the x and y matched, so lets check z
     if(this.zLocation < otherBlock.zLocation)
     {
         return -1;
     }
     else if(this.zLocation > otherBlock.zLocation)
     {
         return 1;
     }
     
     //everything matched, the two blocks are equal
     return 0;
 
 }

Then you could have an list of Blocks and just call Sort() on the list. If you need more details on implementing IComparable I'd check out this tutorial:

http://www.codeproject.com/Articles/42839/Sorting-Lists-using-IComparable-and-IComparer-Inte

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 Calvin_And_Hobbies · Jan 28, 2015 at 03:02 PM 0
Share

Thanks! That looks like it should do what I want.

avatar image Calvin_And_Hobbies · Jan 29, 2015 at 02:47 AM 0
Share

I'm getting a weird error message when I try the code: Assets/Scripts/Editor Scripts/EditorButtons.cs(7,14): error CS0535: Block' does not implement interface member System.IComparable.CompareTo(Block)'

Any ideas?

avatar image LessThanEpic · Jan 29, 2015 at 02:57 AM 0
Share

Change your method signature from

 public int CompareTo(Object otherBlock)

to

  public int CompareTo(Block otherBlock)
avatar image Calvin_And_Hobbies · Jan 29, 2015 at 03:07 AM 0
Share

Ok that bug was fixed, but now I have a new problem. I want to be able to add objects to a new list via button. Can I create a list in one function and add to it in another?

avatar image Calvin_And_Hobbies · Jan 29, 2015 at 03:28 AM 0
Share

Never $$anonymous$$d, I just made the variable at the beginning of the class.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Find index of item in list 1 Answer

Sorting a List of Dictionaries in C#? 4 Answers

Sort list game objects based on name 3 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