Commit be135beb authored by Elena Berrios's avatar Elena Berrios
Browse files

Sort leaderboard scores

parent c6bd7052
Loading
Loading
Loading
Loading
+26 −21
Original line number Diff line number Diff line
package com.example.game2d;

import static com.example.game2d.MapUtil.sortByValue;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class LeaderboardActivity extends AppCompatActivity {

    private LinearLayout scoreListLayout;

    @SuppressLint("DefaultLocale")
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -20,28 +31,22 @@ public class LeaderboardActivity extends AppCompatActivity {
        // Get reference to the LinearLayout that will hold the scores
        scoreListLayout = findViewById(R.id.score_list_layout);

        // Create vectors to store the usernames and scores
        Vector<String> usernames = new Vector<>();
        usernames.add("Alice");
        usernames.add("Bob");
        usernames.add("Charlie");
        usernames.add("Dave");

        Vector<Integer> scores = new Vector<>();
        scores.add(100);
        scores.add(75);
        scores.add(60);
        scores.add(50);

        // Loop through the scores and add them to the layout
        for (int i = 0; i < usernames.size(); i++) {
            String username = usernames.get(i);
            int score = scores.get(i);

            // Create a new TextView to display the score
            TextView scoreView = new TextView(this);
            scoreView.setText(String.format("%s: %d", username, score));
        // Create map to store usernames and scores
        HashMap<String, Integer> scoreMap = new HashMap<String, Integer>();
        scoreMap.put("Bob", 75);
        scoreMap.put("Dave", 50);
        scoreMap.put("Charlie", 60);
        scoreMap.put("Alice", 100);

        // Sort map by values (descending scores)
        HashMap<String, Integer> sortedScoreMap = new HashMap<String, Integer>();
        sortedScoreMap = (HashMap<String, Integer>) sortByValue(scoreMap);

        // Loop through keys (usernames) and add username and score to the layout
        for (String username : sortedScoreMap.keySet()) {
            // Create a textview for the username and score
            TextView scoreView = new TextView(this);
            scoreView.setText(String.format("%s: %d", username, sortedScoreMap.get(username)));
            // Add the TextView to the LinearLayout
            scoreListLayout.addView(scoreView);
        }
+27 −0
Original line number Diff line number Diff line
package com.example.game2d;

import android.os.Build;

import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// https://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values
public class MapUtil {
    @RequiresApi(api = Build.VERSION_CODES.N)
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
        List<Map.Entry<K, V>> list = new ArrayList<>(map.entrySet());
        list.sort(Collections.reverseOrder(Map.Entry.comparingByValue()));

        Map<K, V> result = new LinkedHashMap<>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }

        return result;
    }
}
 No newline at end of file