Search Unity

Sort array by score

Discussion in 'Scripting' started by Mirek Uhlir, Mar 16, 2011.

  1. Mirek Uhlir

    Mirek Uhlir

    Joined:
    Dec 25, 2009
    Posts:
    124
    Hi
    I have an array of scores. I would like to sort this from smallest to largest. To each of the scores should be assigned to the player's name. How can I do it? Should I have two fields - names and scores? When the scores are sorted, the order is changed, then how to assign names to the correct position?
     
  2. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    If you're working in C#, SortedList or SortedDictionary will be the easiest way to go

    Code (csharp):
    1.  
    2. using System.Collections.Generic:
    3.  
    4. SortedList<int, string> highscores;
    5. ...
    6.  
    7. highscores.Add(2000, "Player A");
    8. highscores.Add(3000, "Player B");
    9. highscores.Add(1000, "Player C");
    10. highscores.Add(999, "Player D");
    11.  
    12.  
    However, this will only work if the scores are unique, which is pretty unlikely. Better approach is to create a Score class, like

    Code (csharp):
    1.  
    2. public class Score : IComparable {
    3.    public int score;
    4.    public string playerName;
    5.  
    6.    public Score(int score, string playerName) {
    7.       this.score = score;
    8.       this.playerName = playerName;
    9.    }
    10.  
    11.   public int CompareTo(object obj) {
    12.      Score otherScore = obj as Score;
    13.      if(otherScore!=null) {
    14.         return this.score.CompareTo(otherScore.score);
    15.      } else {
    16.         throw new ArgumentException("Object is not a Score");
    17.      }
    18.   }
    19.  
    20.   public override ToString() {
    21.      return String.Format("{0}: {1}", this.playerName, this.score);
    22.   }
    23. }
    24.  
    25.  
    26. List<Score> highscores = new List<Score>();
    27. highscores.Add(new Score(2000,"Player A"));
    28. highscores.Add(new Score(3000, "Player B"));
    29. highscores.Add(new Score(1000, "Player C"));
    30. highscores.Add(new Score(999, "Player D"));
    31.  
    32. highscores.Sort();
    33.  
    34. for(int i=0;i<highscores.Count; i++) {
    35.    Debug.Log(highscores[i].ToString());
    36. }
    37.  
    38.  
    By implementing IComperable you can define your own sorting criteria in the CompareTo(...) method. Since it should be sorted by score, you just use the Int classes CompareTo function and compare the score of both results. Also see the IComperable MSDN documentation.
     
    Last edited: Mar 16, 2011
  3. Mirek Uhlir

    Mirek Uhlir

    Joined:
    Dec 25, 2009
    Posts:
    124
    Thanks. I have everything written in JS. I try to do this as a function in a separate file. It is possible to make similar solution in JS?
     
  4. Mirek Uhlir

    Mirek Uhlir

    Joined:
    Dec 25, 2009
    Posts:
    124
    I get error
    The modifier `override' is not valid for this item on

    Code (csharp):
    1.  public override ToString() {
    2.      return String.Format("{0}: {1}", this.playerName, this.score);
    3.   }
    What am I doing wrong ?
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Another approach is to use this version of System.Array.Sort, which takes two arrays and sorts them in parallel, based on the order of keys in the first array:-
    Code (csharp):
    1. var scores: float[];  // The nth name corresponds with the nth score.
    2. var names: String[];
    3.   ...
    4.  
    5. System.Array.Sort(scores, names);
     
    DBarlok and Cedebo like this.
  6. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    Oh forgot the return type.

    Should be
    Code (csharp):
    1. public override string ToString() {
     
  7. DBarlok

    DBarlok

    Joined:
    Apr 24, 2013
    Posts:
    268
    jojojo, thats economic! :cool: