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
Anyone? Am I on the right track, or is this not due able?
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
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.
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();
}
}
BTW add it to answer and ill mark it as accepted. Thanks again.