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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:32:17+00:00 2026-05-13T23:32:17+00:00

I have an array which i need to sort its elements by occurrence then

  • 0

I have an array which i need to sort its elements by occurrence then alphabetically.
For example:

55 The
32 ASomething
32 BSomething

ASomething should come before Bsomething because:
1) they have the same number
2) A comes before B alphabetically

So you sort first by the number of occurrence then Alphabetically

What is the best way to do that.
I am using merge sort to sort the counts but how do I put a statement that it will check if they have the same number, it sorts alphabetically (could be more than 2 words).

SOLUTION: What I did is a merge sort on the data before I did a merge sorts on the counts of data and that was good enough 🙂 Thanks everyone for the 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-13T23:32:17+00:00Added an answer on May 13, 2026 at 11:32 pm

    You need a custom Comparator for that using Arrays.sort():

    Arrays.sort(array, new CustomComparator());
    
    public class CustomComparator implements Comparator<String> {
      private final Pattern pattern = Pattern.compile("(\\d+)\\s+(.*)");
    
      public int compare(String s1, String s2) {
        Matcher m1 = pattern.matcher(s1);
        if (!m1.matches()) {
          throw new IllegalArgumentException("s1 doesn't match: " + s1);
        }
        Matcher m2 = pattern.matcher(s2);
        if (!m2.matches()) {
          throw new IllegalArgumentException("s2 doesn't match: " + s2);
        }
        int i1 = Integer.parseInt(m1.group(1));
        int i2 = Integer.parseInt(m2.group(1));
        if (i1 < i2) {
          return 1;
        } else if (i1 > i2) {
          return -1;
        }
        return m1.group(2).compareTo(m2.group(2));
      }
    }
    

    For Collections you can use Collections.sort()

    The above assumes your array elements are Strings like "22 ASomething" rather than a specific data structure containing occurrences and some text. If that is the case you can use a simpler Comparator.

    Also if you do have an array of Strings it might be worth first transforming it into an array of objects that have been parsed to save over-parsing the elements (ie some elements will be parsed more than once).

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

Sidebar

Related Questions

I have an array Items which has 10 elements. I need to remove the
I have an application in which I need to pass an array from C#
I have an array which is converted from an XML response. What I need
I have an ArrayList which I need to sort and also I need a
I need to create a sort of 2D array in which each one of
I have an array which is a list of domains, I want to print
I have an array which contains another array Would I notate it this way?
I have an array which has the contents as the result of an sql
I have an array which contains around 50-200 hyperlinks. How can I pass this
i have an array which could look like this: $config[$name][$array]['key'] = 'value'; // here

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.