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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:55:10+00:00 2026-06-03T02:55:10+00:00

I want to sort a text column, where the numeric components should be sorted

  • 0

I want to sort a text column, where the numeric components should be sorted as numbers. The sorted result should look like this:

chr1
chr1,chr1
chr1,chr2
chr1,chr10
chr2
chr2,chr1
chr2,chr2
chr2,chr10
chr6
chr6,chr1
chr6_ux9
chr6_ux9,chr1
chr7
chr10
chr10,chr1
chr10,chr2
chr10,chr10
chr21
chr21,chr1
chr21,chr2
chr21,chr10
chrx
chrx,chr1
chrx,chr2
chrx,chr10
chry
chry,chr1
chry,chr2
chry,chr10
chrmt
chrmt,chr1
chrmt,chr2
chrmt,chr10
chr25
chr25,chr1
chr25,chr2
chr25,chr10

The following rules apply:

  1. chrx is treated as chr22
  2. chry is treated as chr23
  3. chrmt is treated as chr24
  4. chr6_ux9 is a special case that should come after chr6

I tried different ways but not able to find the perfect solution. Please help me if anyone has an idea.

  • 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-03T02:55:11+00:00Added an answer on June 3, 2026 at 2:55 am

    I think I understand now, what you are looking for. You want the numeric components sorted as numbers, not strings. This should work for you:

    SELECT col1
    FROM   tbl
    ORDER  BY string_to_array(
                 replace(replace(replace(replace(replace(replace(
                    col1
                  , 'chrx',     'chr22')
                  , 'chry',     'chr23')
                  , 'chrmt',    'chr24')
                  , 'chr6_ux9', 'chr6.6')
                  , ' chr',     'chr')
                  , 'chr',      '')
               , ',')::real[];
    

    Sorts columns as depicted in the questin. The textual component (‘chr’) turns out to be redundant noise. After applying all replacements, I strip the noise and cast to a numeric array which can be used in the ORDER BY clause.

    While performing the listed substitutions, the special case for chr6_ux9 forces the use of real[] instead of the simpler and faster int[], because the integer type leaves no room between 6 and 7. You also have one column with a space instead of a comma. I added a substitution for that, too. But that’s probably just a typo. After removing the irrelevant string chr, only comma-separated numbers remain, which can be cast to real[].

    BTW, replace() is very fast. I have functions with dozens of replace() operations in a row that still perform fast. (regexp_replace() is much slower.)


    Alternative answer for sorting individual elements

    For a sorted output of all values as strings:

    SELECT regexp_split_to_table(replace(replace(replace(
              col1
            ,'chrx', 'chr22')
            ,'chry', 'chr23')
            ,'chrmt', 'chr24')
            , ',') AS col1
    FROM   tbl
    ORDER  BY 1
    

    chr6_ux9 comes after chr6 automatically in this scenario.

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

Sidebar

Related Questions

I want to sort a 2-dimensional String array like this xx, text 1 aa,
I want to sort a text file in PowerShell. The text file looks like
I want to sort the paragraphs in my text according to their initials with
I want to sort on a certain column only for rows containing a certain
I want to sort results set first based on one column and then based
I have a column called price and all the values are TEXT so like
I have a table like this I'd like to sort : | Name |
Im having two tables with attributes like date(datetime),headline(varchar),text(text) Now i want to UNION ALL
I have a listview like this and in FormLoad event I should do some
i want to sort dictionary based upon value of each dictioanry item.but if i

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.