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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:33:37+00:00 2026-06-10T19:33:37+00:00

simple question, don’t know if it gets a simple answer. Is there a way

  • 0

simple question, don’t know if it gets a simple answer.
Is there a way to sort a list of string wich contains letters and numbers, but considering the numbers as well?

For example, my list contains:

(1) ["Group 1", "Group2", "Group3", "Group10", "Group20", "Group30"]

(The strings doesn’t have the word “group” necessarily, it could have others words)

if I sort it, it shows:

(2)
Group 1
Group 10
Group 2
Group 20
Group 3
Group 30

Is there a way to sort it like (1) ?

Thanks

  • 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-10T19:33:39+00:00Added an answer on June 10, 2026 at 7:33 pm

    try this:

    def test=["Group 1", "Group2", "Group3", "2", "Group20", "Group30", "1", "Grape 1", "Grape 12", "Grape 2", "Grape 22"]
    
    test.sort{ a,b ->
        def n1 = (a =~ /\d+/)[-1] as Integer
        def n2 = (b =~ /\d+/)[-1] as Integer
    
        def s1 = a.replaceAll(/\d+$/, '').trim()
        def s2 = b.replaceAll(/\d+$/, '').trim()
    
        if (s1 == s2){
            return n1 <=> n2
        }
        else{
            return s1 <=> s2
        }
    }
    
    println test
    

    If you want to compare first the number you have to change the internal if with:

    if (n1 == n2){
        return s1 <=> s2
    }
    else{
        return n1 <=> n2
    }
    

    This take te last number it found in the string, so you can write what do you want, but the ‘index’ should be the last number

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

Sidebar

Related Questions

Hey experts, actually it's a pretty simple question...just don't know about the answer. I
( I know this is probably a simple question to answer, but I don't
So, probably a simple question with a simple answer, but I don't know what
It is a simple question but I don't know how to do it because
This is a simple question but I don't know how to construct a variable
Ok so this maybe a simple/silly question but I don't know so here goes:
I know, maybe it is a simple question, bu I don't know how to
There might be a really simple solution to this question, but i just don't
I think this is a relatively simple question, but I don't precisely know what's
seems like a simple question but I don't know JavaScript too well and my

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.