Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by jimjames · Feb 16, 2016 at 05:00 PM · generic list

Different Kinds of Sort() in 1 Generic List

I am trying to create 2 or more ways to sort a generic list.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 
 [System.Serializable]
 public class HighScore : IComparable<HighScore>
 {
     //creating 1st custom sorting order
     public int CompareToScore(HighScore other)
     {
         if (this.score > other.score)
             return 1;
         else if (this.score < other.score)
             return -1;
         else
             return 0;
     }
 
     //creating 2nd custom sorting order
     public int CompareToTime(HighScore other)
     {
         if (this.timePlayed > other.timePlayed)
             return 1;
         else if (this.timePlayed < other.timePlayed)
             return -1;
         else
             return 0;
     }
 
     public int score;
     public float timePlayed;
 
     public HighScore(int s, float t)
     {
         score = s;
         timePlayed = t;
     }
 }
 
 public class abcd : MonoBehaviour
 {
     public List<HighScore> highscores = new List<HighScore>();
 
     void Start()
     {
         highscores.Add(new HighScore(2345, 7.2f));
         highscores.Add(new HighScore(3456, 1.6f));
         highscores.Add(new HighScore(5432, 4.65f));
         highscores.Add(new HighScore(7654, 3.5f));
         highscores.Add(new HighScore(4567, 9.45f));
     }
 
     void SortScore()
     {
         //need help on this part
         highscores.Sort(CompareToScore);
         highscores.Sort.SortScore();
     }
 
     void SortTime()
     {
         //need help on this part
         highscores.Sort(CompareToTime);
         highscores.Sort.SortTime();
     }
 }

Thanks: James

Comment
Add comment · Show 2
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 jimjames · Feb 17, 2016 at 02:41 PM 0
Share

Anyone? Am I on the right track, or is this not due able?

avatar image Glurth · Feb 17, 2016 at 03:44 PM 0
Share

Oops, posted wrong stuff at first: edited...

The IComparer class that you derived from expects you to create a function called "Compare", in order to be used with Sort(). (CompareToX and CompareToY, wont work).
Generally one would create TWO(or more) IComparer derived classes, one for each type of Sorting you want to do.
Also note, I see you are storing data in the Icomparer derived class, this is inadvisable. This class should be used PURELY for defining a compare function. Note in this example: they are store a list of strings, but the data is NOT kept in the IComparerer class (it's kept in the string class). https://msdn.microsoft.com/en-us/library/234b841s(v=vs.110).aspx

1 Reply

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

Answer by Nerevar · Feb 17, 2016 at 02:55 PM

What are you looking for exactly? different ways to call your custom sort methods?

If that's it then you could just use Linq Library and you dont need to implement IComparable to do sorts on int or floats.

  void SortScore()
 {
   highscores.OrderByDescending(x => x.score);
 }
 
  void SortTime()
 {
         highscores.OrderByDescending(x => x.timePlayed);
 }

or ideas to sort the highscores on extra attributes?

Your class has only 2 attributes at the moment : "score" and "timePlayed". I have no idea about your game so I can't just randomly suggest game data (FragCount? Kda? Accuracy?) that could be relevant to sort.

Comment
Add comment · Show 2 · 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 jimjames · Feb 17, 2016 at 04:58 PM 0
Share

This is exactly what I was looking for. Thank you so much, but I had to make a small change:

 highscores = highscores.OrderByDescending(x => x.score).ToList();

Here is the full script for anyone else:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 [System.Serializable]
 public class HighScore
 {
     public int score;
     public float timePlayed;
 
     public HighScore(int s, float t)
     {
         score = s;
         timePlayed = t;
     }
 }
 
 public class abcd : $$anonymous$$onoBehaviour
 {
     public List<HighScore> highscores = new List<HighScore>();
 
     void Start()
     {
         highscores.Add(new HighScore(5432, 4.65f));
         highscores.Add(new HighScore(2345, 7.2f));
         highscores.Add(new HighScore(3456, 1.6f));
         highscores.Add(new HighScore(7654, 3.5f));
         highscores.Add(new HighScore(4567, 9.45f));
 
         StartCoroutine(Sort());
     }
 
     void SortScore()
     {
         //highscores = highscores.OrderByDescending(x => x.score).ToList(); //sort the list from highest to lowest
         highscores = highscores.OrderBy(x => x.score).ToList(); //sort the list from lowest to highest
     }
 
     void SortTime()
     {
         highscores = highscores.OrderByDescending(x => x.timePlayed).ToList(); //sort the list from highest to lowest
         //highscores = highscores.OrderBy(x => x.timePlayed).ToList(); //sort the list from lowest to highest
 
     }
 
     IEnumerator Sort()
     {
         yield return new WaitForSeconds(5);
         SortScore();
         yield return new WaitForSeconds(5);
         SortTime();
     }
 }
avatar image jimjames · Feb 17, 2016 at 05:01 PM 0
Share

BTW add it to answer and ill mark it as accepted. Thanks again.

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

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

[SOLVED] ScriptableObject generic list makes all derived instances to base type when serializing 1 Answer

C# - Two lists linked with the same value 1 Answer

Questions regarding IComparer 1 Answer

Help with populating a list 0 Answers

List of active missions 0 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