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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:16:06+00:00 2026-06-14T21:16:06+00:00

EDIT: The comparator doesn’t seem to be doing anything. I get no errors but

  • 0

EDIT: The comparator doesn’t seem to be doing anything. I get no errors but the (already alphabetically sorted) array isn’t changing. I should expect to see SAD and PAD beside each other but I am not.

I have an array of strings and I want to sort them based on their numerical value derived from how to type them out on a dial pad. That is, “SAD” = 723, “PAD” = 723, “SAP” = 727. I have an alphabetically ordered dictionary and I want to rearrange it into my order in order to do a binary search on the array. For things like SAD and PAD being the same value, they can be then sorted alphabetically. I want to try to use Arrays.sort(dictionary, myComparator) to sort it.

First, would this be the best way to go about getting what I want? How else might I get this?

Second, how do I make my own comparator? Note that I am just in a first course in programming so we aren’t even making classes yet, it’s all just a bunch of stuff crammed into a main class. And I generally don’t know about classes at all. So if I would have to go and make my own class to make this work, then I will probably have to code a quicksort myself instead. I haven’t covered linked lists either so my data structure is a bit clumsy. Here is what I have so far and I have no idea what I am doing:

Stupid clumsey way to get the dictionary into memory, important line is near bottom:

public static String[][] dictToMem()throws FileNotFoundException{
    File myFile = new File("src/words.txt");
    Scanner input = new Scanner(myFile);
    String temp;
    String[] tempDict = new String[7];
    Arrays.fill(tempDict, "");

    while (input.hasNext()){
        temp = input.next();
        if (temp.length() < 8 && temp.indexOf('\'') + temp.indexOf('(') +   temp.indexOf('-') == -3){
            for (int i = 1; i <= 7; i++){
                if (temp.length() == i){
                    tempDict[i-1] += " " + temp;
                }
            }
        }
    }
    for (int i = 0; i<tempDict.length; i++){
        tempDict[i] = tempDict[i].trim();
    }

    String[][] dictionary = new String[7][];
    for (int i = 0; i<tempDict.length; i++){
        dictionary[i] = tempDict[i].split(" ");
    }
    for (int i = 0; i<tempDict.length; i++){
        //  ***This is the line doesn't work, compiler error***
        //  dictionary[i] = Arrays.sort(dictionary[i], String.DialPadNumCompare);
        //  Should be this:
        Arrays.sort(dictionary[i], DialPadNumCompare);
    }
    return dictionary;
}

And the comparator isn’t showing an error:

public static Comparator<String> DialPadNumCompare = new Comparator<String>(){
    @Override
    public int compare(String a, String b){
        if (stringToInt(a) == stringToInt(b)){
            return a.compareTo(b);
        }
        return stringToInt(b) - stringToInt(a);
    }
};

String to int is my string to dial pad number converting method

  • 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-14T21:16:07+00:00Added an answer on June 14, 2026 at 9:16 pm

    Your comparator is not correct. Change it to:

    public static Comparator<String> dialPadNumCompare = new Comparator<String>(){
        @Override
        public int compare(String a, String b){
            int inta = stringToInt(a);
            int intb = stringToInt(b);
            if (inta == intb)
                return a.compareTo(b);
            return inta - intb;
        }
    };
    

    And call this comparator using this code:

    Arrays.sort(dictionary, dialPadNumCompare);
    

    Here is Live Demo

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

Sidebar

Related Questions

I get following compilation errors: The method sort(List<T>, Comparator<? super T>) in the type
I have this comparator function for my qsort in C, but I seem to
EDIT: Parsed the array completely (it contained unparsed JSON!) array(22) { [0]=> array(4) {
ok I was going to edit my previous question but i wasnt sure if
Hi want to get the place name from the edit text and mark on
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
EDIT: I was an idiot. I simply had an image that was vertically long,
EDIT: See my answer below--> I am wanting to have a view that when
Edit (updated question) I have a simple C program: // it is not important
EDIT: iam using ajax to load text in my content that is why onload

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.