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
5
Question by Borzi · Mar 30, 2014 at 09:55 PM · networkinglistssortinglinqplayers

Sorting a List by Variable?

Hey! I am trying to sort a list of players for my networked game by "score". This is a variable in the player class. Can someone tell me how I could do this? I want the player with the highest score to be nr 1 and so on.

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

Answer by Bunny83 · Mar 30, 2014 at 10:14 PM

Just write a compare method like this:

     // C#
     static int SortByScore(PlayerClass p1, PlayerClass p2)
     {
         return p1.score.CompareTo(p2.score);
     }

And then sort your List like this:

     playerList.Sort(SortByScore);

If you want you can of course use a lambda-expression like this:

     playerList.Sort((p1,p2)=>p1.score.CompareTo(p2.score));
Comment
Add comment · Show 9 · 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 Bunny83 · Mar 30, 2014 at 10:20 PM 0
Share

*fixed a typo ;)

avatar image Borzi · Mar 30, 2014 at 10:21 PM 0
Share

Thank you for your reply! Is there a more conventional way when there are more than 2 players? Because my game probably supports up to 8...I also have another question. I am using

 public List<Player> PlayerList = new List<Player> ();

Would this be compatible with this method?

avatar image Bunny83 · Mar 30, 2014 at 10:27 PM 0
Share

Uhm, that works for any number of players. The delegate is just the comparison function. The Sort method sorts the whole list based on the compare function.

avatar image Borzi · Mar 30, 2014 at 10:30 PM 0
Share

Okay thanks xD I am going to have to check the reference etc. to understand this a bit better

avatar image Bunny83 · Mar 30, 2014 at 10:44 PM 0
Share

The Sort method takes an optional delegate which is used as compare method. The return value tells the sorting algorithm is p1 is greater than p2, equal or smaller. It returns a value:

  • greater than 0 if p1 is greater than p2

  • of 0 if p1 equals p2

  • smaller than 0 if p1 is smaller than p2

Depending on the sort algorithm the Sort method calls the delegate repeatedly for various pairs to get the elements sorted. Depending on the element count Sort might use the Heapsort, QuickSort or other algorithms.

Show more comments
avatar image
0

Answer by jbvobling · Aug 03, 2021 at 04:32 AM

I encountered some data that is not sorted (I have more than 250 object data) when using name for sorting in an Object Data.

playerList.Sort((p1,p2)=>p1.name.CompareTo(p2.name));

To solve this, I tried to process this 5x playerList.Sort((p1,p2)=>p1.name.CompareTo(p2.name)); playerList.Sort((p1,p2)=>p1.name.CompareTo(p2.name)); playerList.Sort((p1,p2)=>p1.name.CompareTo(p2.name)); playerList.Sort((p1,p2)=>p1.name.CompareTo(p2.name)); playerList.Sort((p1,p2)=>p1.name.CompareTo(p2.name)); and the sort result was success. in ascending order.

Just add another same process if there are still some unsorted data if using more data.

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

25 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

Related Questions

Networking | Getting buffered other - previous spawned players 1 Answer

How to make host unavailable - Unity Networking 1 Answer

Sort a generic list by member 1 Answer

Compare two Lists and get the items that are not in both lists 2 Answers

Unity networking tutorial? 6 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