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 MinceD · Jul 17, 2010 at 06:15 PM · sorting

Sorting an Arraylist filled with structs.

So, I have a struct like this:

public static struct Score { public string Name; public int HighScore; }

and i place a number of these in an arraylist.

I know there is a sort method attahced to the arraylist, but how do I use it?

I would like to sort the arraylist based on the highscore, but I can't seem to figure out how to do it in an easy way.

Any hints would be great!

it would be nice to be able to do something like this:

HighScoreList.Sort(Score.HighScore);

Thanks. :)

Kjetil

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

Answer by Mike 3 · Jul 17, 2010 at 07:01 PM

Something like this should do it:

public class StructComparer : IComparer { public int CompareStructs(object x, object y) { if (!(x is YourStruct) || !(y is YourStruct)) return 0; YourStruct a = (YourStruct)x; YourStruct b = (YourStruct)y;

     return a.HighScore.CompareTo(b.HighScore);
 }

}

//when you want to sort... yourArrayList.Sort(new StructComparer());

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

Answer by StephanK · Jul 17, 2010 at 06:53 PM

I don't think you can sort structs. For sorting to work in C# you have to implement the IComparer interface, which basically is done by overriding the Compare function. As structs can't have methods you'll have to use a class for that.

Comment
Add comment · Show 4 · 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 Mike 3 · Jul 17, 2010 at 07:03 PM 1
Share

$$anonymous$$ost of that isn't true at all. You can sort structs, you don't need to implement the IComparer interface, and structs can have methods (Not that you even need to add your comparer into the struct)

avatar image StephanK · Jul 17, 2010 at 08:58 PM 0
Share

Hm ok, maybe I should read the c# docs more often/carefully and don't assume that concepts are the same in every language, just because they have the same name... What is the purpose of a struct in C# then? (just curious)

avatar image Mike 3 · Jul 18, 2010 at 01:08 AM 0
Share

Value type ins$$anonymous$$d of a reference type, mainly. makes using Vector3 and Quaternion a heck of a lot easier

avatar image Bunny83 · Feb 01, 2011 at 07:27 AM 0
Share

The only language that came to my $$anonymous$$d is Delphi/Pascal where a struct (it's called record in Pascal) can't have methods. Nearly all C derived languages allow methods in structs (at least if they have structs, not like Java)

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

Is not a memeber of Object 1 Answer

Sorting an Transforms array by gameobject name 1 Answer

Sort by Sibling Index 2 Answers

SQLite Sort Ascending order 0 Answers

Grouping neighbouring entrys with the same value in a 2d array. 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