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

  • Home
  • SEARCH
  • 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 5988847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:01:00+00:00 2026-05-22T23:01:00+00:00

i have a string array consisting of a name and a score. I want

  • 0

i have a string array consisting of a name and a score. I want to sort that array by score. Problem is, considering it’s a string array, the scores are strings which results in 13,16,2,5,6 and not 2,5,6,13,16. I am using this code:

int spaceIndex;
String[][] scoreboard;
String[] playername;
String[] score;
int sbsize;

array1.add("Thomas" + ":" + 5);
array1.add("Blueb" + ":" + 6);
array1.add("James" + ":" + 16);
array1.add("Hleb" + ":" + 13);
array1.add("Sabbat" + ":" + 2);
sbsize = array1.size();
scoreboard = new String[sbsize][2];
playername = new String[sbsize];
score = new String[sbsize];
pos2 = new int[sbsize];

for (int i=0; i<array1.size(); i++)
{
    spaceIndex =  array1.get(i).indexOf(':'); 
    scoreboard[i][0] = array1.get(i).substring(0, spaceIndex);
    scoreboard[i][1] = array1.get(i).substring(spaceIndex+1, array1.get(i).length());
}

Arrays.sort(scoreboard, new Comparator<String[]>() {
 @Override
 public int compare(String[] entry1, String[] entry2) {
    String time1 = entry1[1];
    String time2 = entry2[1];
    return time1.compareTo(time2);
    }
 });

What is the solution?

  • 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-22T23:01:01+00:00Added an answer on May 22, 2026 at 11:01 pm

    Cast them to int. As I recall, something like…

    Arrays.sort(scoreboard, new Comparator<String[]>() {
         @Override
         public int compare(String[] entry1, String[] entry2) {
            Integer time1 = Integer.valueOf(entry1[1]);
            Integer time2 = Integer.valueOf(entry2[1]);
            return time1.compareTo(time2);
            }
         });
    

    Also you can make simple value object class for easier manipulations. Like…

    class Player
    {
      public String name;
      public int score;
    }
    

    And after that you can make

     Player[] scoreboard = ...
     Arrays.sort(scoreboard, new Comparator<Player>() {
              @Override
              public int compare(Player player1, Player player2) {
                  if(player1.score > player2.score) return 1;
                  else if(player1.score < player2.score) return -1;
                  else return 0;            
                 }
     });
    

    Edit:
    I recommend you to understand the basic OOP principles, this will help you a lot in the beginning.

    Edit 2: Java 8 (with functional interface and a lambda):

    Arrays.sort(scoreboard, (player1, player2) -> {
      Integer time1 = Integer.valueOf(player1[1]);
      Integer time2 = Integer.valueOf(player2[1]);
      return time1.compareTo(time2);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an array of strings I am displaying the following data in
I am passing back a JSON object (consisting of an array of strings) from
I have a method which reads a file and returns the contents of the
Lets say I have string like this: String q = foo (one) bla (two)
I have written a short Scala program to read a large file, process it
I've got an in page text search using JS, which is here: $.fn.eoTextSearch =
I have some unusual I need to do. I am wondering if anyone can
I wonder if anyone could offer their advice on this one. I have some
This is a stupid question, it feels like one. But mental block is bad
I'm working with CakePHP and trying to understand the best ways to make my

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.