Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7046011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:35:59+00:00 2026-05-28T02:35:59+00:00

I am developing a game for android and I save the scores in a

  • 0

I am developing a game for android and I save the scores in a text file of the form “100&playername1,93&playername1,1950&playername2” etc. i.e. it is totally unordered.

Now I am trying to make a high score interface and I am aware that to sort the scores I should use String.split(",") followed by String.split("&")[0] to get the scores and put these in an ArrayList and then call Collections.compare(list). However once I have done that I then have no way of finding the names associated with the score.

Could anyone help me with this please. I have tried sorting the whole string in between brackets (putting the phrase “100&playername1” into the array, but that can’t sort according to orders of magnitude. By this I mean it would put 100 ahead of 1950.

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:36:00+00:00Added an answer on May 28, 2026 at 2:36 am

    Make a class called UsernameScorePair. Once you have split the scores and the usernames, put them in pairs (one for each “score&username”).

    For example, this class definition could work:

    public class UsernameScorePair {

        private String name;
        private int score;
    
        public UsernameScorePair(String name, int score) {
            this.name = name;
            this.score = score;
        }
    
        public String getName() {
            return name;
        }
    
        public int getScore() {
            return score;
        }
    
    }
    

    Make a class called UsernameScorePairComparator that implements Comparator.

    In the compare(Object o1, Object o2) method, cast the Objects to UsernameScorePairs and compare the scores. For example:

    public class UsernameScorePairComparator {
    
        public UsernameScorePairComparator() {
        }
    
        public int compare(Object o1, Object o2) {
            UsernameScorePair usp1 = (UsernameScorePair)o1;
            UsernameScorePair usp2 = (UsernameScorePair)o2;
    
            return usp1.getScore() - usp2.getScore();
        }
    }
    

    Then use Collections.sort like this:

    Collections.sort(listofpairs, new UsernameScorePairComparator())

    I don’t remember if it sorts in ascending order or descending order. If it’s ascending order, then just change return usp1.getScore() - usp2.getScore(); to return usp2.getScore() - usp1.getScore();

    EDIT

    A Comparator basically compares two objects. In its compareTo method, it returns a negative value if the first is less than the second, a positive one if the first is greater than the second, and zero if they are both equal.

    You can implement a Comparator (as I just did) to suit your needs. Then, using that Comparator, you can use standard API methods such as Collections.sort().

    I hope this works!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing chess game for Android ( androidchess.appspot.com ). If I want to add
I'm currently developing a small OpenGL game for the Android platform and I wonder
Im developing a game for Android using AndEngine. As of now, I want to
im developing a game in android, and i've been thinking about subdividing many of
I'm developing a game for android and this is my first experience with OpenGL.
i'm developing a android game which uses libgdx and bullet physics engine. I can
I am developing an android game, in which i have some trains moving on
I'm developing an Android game that has to download some assets to the SD
I am developing a 2D game for android using lots of sprites. I am
I m developing a card game in android. i need to show three cards

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.