chArm
chArm

Reputation: 35

sorting an array of score(int) along with its corresponding name(string)

I am making a scrabble app and I want to make a top score system. I started making an array where the names of the players and their corresponding scores are saved. I want to sort the array of scores so that they are arranged from highest to lowest and output the scores (in descending order) along with the corresponding names. Please guide me on how to sort the scores and output them along with the corresponding player names. Any help would be much appreciated. Thank you!

here's what I have so far:

public class MainTop extends Activity {

TextView show;
EditText name;
EditText score;
Button ok;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layouttop);

    show = (TextView) findViewById(R.id.textView1);
    name = (EditText) findViewById(R.id.editText1);
    score = (EditText) findViewById(R.id.editText2);

    ok = (Button) findViewById(R.id.button1);

    ok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        String [] namearray = new String [4];
        int [] scorearray = new int[4];

        for (int i = 0; i < 4; i++){
            String playername = name.getText().toString();
            namearray[i] = playername;

            String playerscorestr = score.getText().toString();
            int playerscore = Integer.parseInt(playerscorestr);
            scorearray[i] = playerscore;

            show.setText(namearray[i] + scorearray[i]);

        }

Upvotes: 0

Views: 1356

Answers (3)

Pragnani
Pragnani

Reputation: 20155

I don't where do you relate scores and players but If your scores and players are like this than you can try this

        int[] scores={10,50,20,60};
        String[] players={"player1","player2","player3","player4"};
        Hashtable<String, String> playerdetails=new Hashtable<String, String>();
        ArrayList scoreslist=(ArrayList) Arrays.asList(scores);
        for(int i=0;i<scores.length;i++)
        {
            playerdetails.put(String.valueOf(scores[i]), players[i]);
        }
        Collections.sort(scoreslist);
        for(int i=0;i<scoreslist.size();i++)
        {
            show.setText(scoreslist.get(i)+"    "+playerdetails.get(scoreslist.get(i)));
        }

Upvotes: 0

kyogs
kyogs

Reputation: 6846

Make player class like this

public class LabelModel {

    public ArrayList<LabelModel> labelModels;
    public String labelId, labelName;

    public ArrayList<LabelModel> getLabelModels() {
        return labelModels;
    }

    public void setLabelModels(ArrayList<LabelModel> labelModels) {
        this.labelModels = labelModels;
    }

    public String getLabelId() {
        return labelId;
    }

    public void setLabelId(String labelId) {
        this.labelId = labelId;
    }

    public String getLabelName() {
        return labelName;
    }

    public void setLabelName(String labelName) {
        this.labelName = labelName;
    }

}

and set array list like

for (int i = 0; i < length; i++) {
        labelModel = new LabelModel();
        JSONObject labeljson = jsonArray.getJSONObject(i);
        labelModel.setLabelId("id");
        labelModel.setLabelName("player name");
        labelarray.add(labelModel);
        }

Upvotes: 1

npinti
npinti

Reputation: 52205

It might be best if you create a class which represents a Player. It would have properties such as score, name, etc and then, you would make your Player class implement the Comparable interface so that you can specify how will the object be sorted.

Once you have that, you could create a List of players and use Collections.sort() to sort your Player objects.

Upvotes: 4

Related Questions