按对象中的值对对象进行排序

Oli*_*ack 0 java arrays sorting android

我有一个对象数组,这些对象都有多个存储在其中的值.我把它们放在一个对象数组中.

class PlayerScores {

String playerName;
   int pos=0;
   int played=0;
   int win=0;
   int draw=0;
   int lose=0;
   int goalsFor=0;
   int goalsAgainst=0;
   int goalDifference=0;
   int points=0;

   public PlayerScores() {
   }
}
Run Code Online (Sandbox Code Playgroud)

这些存储在数组中:

Player Scores[] playersObjects = new PlayerScores[int];

            playersObjects[i] = new PlayerScores();
Run Code Online (Sandbox Code Playgroud)

我想通过'playersObject []'进行搜索然后在一个新的对象数组中进行排序,其中数组中的最高点为Player,其余的按降序排列.我不确定如何对对象中的单个值运行排序.

任何帮助都会受到大力赞赏,

谢谢.

kab*_*uko 6

您可以使用Arrays.Sort并提供自定义Comparator.这样的事情应该有效:

public class PlayerScoresComparator implements Comparator<PlayerScores> {
    @Override
    public int compare(PlayerScores lhs, PlayerScores rhs) {
        return lhs.points - rhs.points;
    }
}
Run Code Online (Sandbox Code Playgroud)