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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:44:05+00:00 2026-06-12T00:44:05+00:00

what im trying to do here is to sort the regex findings(for example if

  • 0

what im trying to do here is to sort the regex findings(for example if they was numbers). im not to sure how to go about doing this, any ideas?

NodeList abcList = firstElement.getElementsByTagName("target");
Element abcElement =(Element)abcList.item(0);
NodeList textAbcList = abcElement.getChildNodes();
String abc = (textAbcList.item(0).getNodeValue().trim());
Pattern pattern = Pattern.compile("Some Regex");
Matcher matcher = pattern.matcher(abc);
while (matcher.find()){
out.write(" abc: " + matcher.group());
}
  • 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-12T00:44:06+00:00Added an answer on June 12, 2026 at 12:44 am

    Finding

    To sort results you need to find them all first. You can produce any partial sorted list if you don’t know all the results beforehand. So you’ll have something like:

    List<Integer> results = new ArrayList<Integer>();
    while (there are more results) { // here you ask the regex if it found some more item
       // add integer to results
       String found = ... // here you grab the string you've just found
       results.add(Integer.parseInt(found)); // convert the string to integer and add to list
    }
    

    Note that I’m converting the found string directly to Integer because it has more meaning as an Integer. If by any reason you want to have a string, ok, have a List<String> and don’t convert.

    Sorting

    After you have a non-sorted list, you need to sort it. There are several methods and Java implements one very easy method. It can do sorting of any type because it doesn’t do the compare between two items. This is the only part that needs to be implemented to define HOW to sort. And you’ll do:

    Collections.sort(results, comparator);
    

    This method will implement mergesort (if I’m not mistaken) and ask the comparator you provide each time it needs to compare two elements. This comparator should implement interface Comparator<T> where T is the type of elements in result.

    If they are integers, you don’t need a comparator because it has already a “natural” order:

    Collections.sort(results);
    

    But if you want some special ordering (like ordering strings according to its integer represented value) then you can use your own comparator:

    Collections.sort(results, new Comparator<String>() {
       public int compare(String a, String b) {
          int valueA = Integer.parseInt(a);
          int valueB = Integer.parseInt(b);
          return valueA - valueB;
       }
    });
    

    compare must return:

    • negative if a < b
    • 0 if a == b
    • and positive if a > b.

    As we want to compare strings as if they are numbers, that’s what I did: convert them to numbers and compare their numerical value.

    Sorting your strigs: xxx-nnnn-nnnn

    In your case you are collecting strings with that format (abc-1234-5678) and you need to sort them according to the first number. So let’s assume you’ve already collected your strings in:

    List<String> results
    

    Then you need to sort that strings according to some arbitrary criteria. As usual you’ll need to call Collections.sort providing a special comparator.

    That comparator will need to compare not the whole string, but the first number from each string. By example: abc-1234-5678 and def-3456-1988. You have to compare 1234 with 3456.

    Then the code will look something like:

    Collections.sort(results, new Comparator<String>() {
      public int compare(String str1, String str2) {
         // obtain the number you'll use to compare
         int value1 = getImportantNumber(str1);
         int value2 = getImportantNumber(str2);
         // return comparator (remember, the sign of the results says if it's <, =, >)
         return value1 - value2;
      }
    
      // this method will extract the number, maybe you'll need a regex or substring, dunno
      private int getImportantNumber(String str) {
         // by example
         Matcher m = PATTERN.matcher(str);
         if (!m.find())
            return -1; // or throw an exception, depends on you're requirements
         String numberPart = m.group(...); // the number of the group catching the part you need
         return Integer.parseInt(numberPart);
      }
    
      private static Pattern PATTERN = Pattern.compile("...."); 
    });
    

    Which regex

    I should use:

    (\w+)-(\d+)(-(\d+))*
    

    That finds:

    letters-numbers[-numbers[-numbers...]]
    

    But if you’r not sure of finding the numbers on the second place I should go for:

    String[] parts = str.split("-");
    for (String part: parts)
       if (this part has only numbers)
          return Integer.parseInt(part);
    // if there are no only number parts
    throw new RuntimeException("Not valid number part found!");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to sort unordered images into correct sequence. Here i am not
I am trying to sort my posts in my discussion board by date. Here
I can't seem to find an example of what I'm trying to do here
I am trying to sort a multidimensional JSON array (called jsontest here) using JQuery
I am trying to write merge sort and stuck here. What is the problem
I am trying to make a sort of enum. Here is my implementation: #
What Im Trying to do here is set up some sort of filter. I
I am trying to implement the bubble sort algorithm in C. Here is what
Ok, here's what I'm trying to do... I know that itemgetter() sort could to
I am trying to do a sort here to display the latest entries first.

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.