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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:52:18+00:00 2026-06-06T14:52:18+00:00

I have a List of Strings. EXAMPLE_1, EXAMPLE_2, EXAMPLE_3 … EXAMPLE_99 What is the

  • 0

I have a List of Strings. EXAMPLE_1, EXAMPLE_2, EXAMPLE_3 … EXAMPLE_99
What is the best algorithm for sorting here?

Is it possible with a Collator?
This is my current procedure, but I guess there could be a better way:

public class Example implements Comparable<Example> {
    private final String id;

    public getId() {
        return id;
    }

    private Integer getIdNo() {
        try {
            return Integer.parseInt(getId().replaceAll("[\\D]", ""));
        } catch (NumberFormatException e) {
            return null;
        }
    }

    @Override
    public int compareTo(Example o) {
        if ((getIdNo() == null && getIdNo() != null) || (getProductFeatureId_sizeNo() < o.getProductFeatureId_sizeNo())) {
            return -1;
        } else if (o.getIdNo() == null || getIdNo() > o.getIdNo()) {
            return 1;
        }

        return 0;
    }
}
  • 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-06T14:52:19+00:00Added an answer on June 6, 2026 at 2:52 pm

    This is better alternative – AlphanumComparator.java

    Copying the code for ready reference –

    public class AlphanumComparator implements Comparator
    {
        private final boolean isDigit(char ch)
        {
            return ch >= 48 && ch <= 57;
        }
    
        /** Length of string is passed in for improved efficiency (only need to calculate it once) **/
        private final String getChunk(String s, int slength, int marker)
        {
            StringBuilder chunk = new StringBuilder();
            char c = s.charAt(marker);
            chunk.append(c);
            marker++;
            if (isDigit(c))
            {
                while (marker < slength)
                {
                    c = s.charAt(marker);
                    if (!isDigit(c))
                        break;
                    chunk.append(c);
                    marker++;
                }
            } else
            {
                while (marker < slength)
                {
                    c = s.charAt(marker);
                    if (isDigit(c))
                        break;
                    chunk.append(c);
                    marker++;
                }
            }
            return chunk.toString();
        }
    
        public int compare(Object o1, Object o2)
        {
            if (!(o1 instanceof String) || !(o2 instanceof String))
            {
                return 0;
            }
            String s1 = (String)o1;
            String s2 = (String)o2;
    
            int thisMarker = 0;
            int thatMarker = 0;
            int s1Length = s1.length();
            int s2Length = s2.length();
    
            while (thisMarker < s1Length && thatMarker < s2Length)
            {
                String thisChunk = getChunk(s1, s1Length, thisMarker);
                thisMarker += thisChunk.length();
    
                String thatChunk = getChunk(s2, s2Length, thatMarker);
                thatMarker += thatChunk.length();
    
                // If both chunks contain numeric characters, sort them numerically
                int result = 0;
                if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0)))
                {
                    // Simple chunk comparison by length.
                    int thisChunkLength = thisChunk.length();
                    result = thisChunkLength - thatChunk.length();
                    // If equal, the first different number counts
                    if (result == 0)
                    {
                        for (int i = 0; i < thisChunkLength; i++)
                        {
                            result = thisChunk.charAt(i) - thatChunk.charAt(i);
                            if (result != 0)
                            {
                                return result;
                            }
                        }
                    }
                } else
                {
                    result = thisChunk.compareTo(thatChunk);
                }
    
                if (result != 0)
                    return result;
            }
    
            return s1Length - s2Length;
        }
    }
    

    Note: you should generify this class if you’re using java 1.5+

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

Sidebar

Related Questions

I have list of strings like this FirstName-Lastname (separated by a dash -) I
I have a list of strings that look like this: abc|key1|486997 def|key1|488979 ghi|key2|998788 gkl|key2|998778
I have a list of strings similar to this list: tags = ('apples', 'apricots',
I have a list of strings like this: my_list = ['Lorem ipsum dolor sit
I have several lists of strings like so, from a possible list of several
I have a .txt file,primary list, with strings like this: f r y h
Example of the problem If I have a list of valid option strings which
I have a list of strings in my view model. To edit them, I
I have sorted list of strings that I move between php and java. to
I have a list of strings that I need to pass to a process

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.