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 9076965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:13:39+00:00 2026-06-16T19:13:39+00:00

I think many people have come across this problem, You have a 2D array

  • 0

I think many people have come across this problem,
You have a 2D array consisting of a String conponent and an Interger.
ex.

    `String[][] data = {{"Name1","5"}, {"Name2","10"},{"Name3","1"}, {"Name4","3"}};`

Now you want to sort that 2D array by the integer (in this case the players score), however you want the matching name to be moved along with the players score.
This is what i’ve got, but the result is far away from what it is suppose to be.

    private void sort(){
    boolean sort;
    int current, next;
    do{
            sort = true;
            for (int i = 0; i < data.length - 1; i++){
                if (data[i][1] !=null && data[i+1][1] != null){
                    current = Integer.parseInt(data[i][1]);
                    next = Integer.parseInt(data[i+1][1]);
                        if(current > next){
                                String temp = "";
                                data[i][1] = Integer.toString(next); 
                                data[i+1][1] = Integer.toString(current);
                                data[i][0] = temp;
                                data[i][0] = data[i+1][1];
                                data[i+1][0] = temp;       
                                sort = false;
                        }
                }

            }

    }while(!sort);
}

If you ask why people would use a common 2D array, it is because in JFRAME, a JTable needs an 2D array for the data.

  • 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-06-16T19:13:40+00:00Added an answer on June 16, 2026 at 7:13 pm

    You can write a special Comparator, wich can compare e.g.
    {"Name1","5"} and {"Name2","10"}
    But I recomment to change your data structure.

    I would create a type caonatining name and int

    class NameNum{
       String name;
       int number;
    }
    

    and store them in a 1D Array

    NameNum[] data 
    

    Implement a compare method and than simply use Arrays.sort(..) to sort the array.

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

Sidebar

Related Questions

I know many people have had this problem and I have searched Google/Stack Overflow,
I have been stressed out with many responses about this issue and many people
I think many people have done some similar development tasks before: I would like
Many people have faced similar issues, and I think I have followed and fixed
Like many people already seem to have (there are several threads on this subject
I think this question has been asked many a times but I've been searching
There are many errors here in SO, but this scenario I think its different.
I've published my website many times. But didn't think about this though until I
A recent question contains a problem that I many times used to think about
I really nead to ask you this one, many of you might think it's

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.