Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I <em>think</em> I understand now, what you are looking for. You want the <strong>numeric components sorted as numbers</strong>, not strings. This should work for you:</p> <pre><code>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[]; </code></pre> <p>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 <code>ORDER BY</code> clause.</p> <p>While performing the listed substitutions, the special case for <code>chr6_ux9</code> forces the use of <code>real[]</code> instead of the simpler and faster <code>int[]</code>, because the <code>integer</code> type leaves no room between <code>6</code> and <code>7</code>. 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 <code>chr</code>, only comma-separated numbers remain, which can be cast to <code>real[]</code>.</p> <p>BTW, <a href="http://www.postgresql.org/docs/current/interactive/functions-string.html#FUNCTIONS-STRING-OTHER" rel="nofollow"><code>replace()</code></a> is very fast. I have functions with dozens of <code>replace()</code> operations in a row that still perform fast. (<code>regexp_replace()</code> is much slower.)</p> <hr> <h3>Alternative answer for sorting individual elements</h3> <p>For a sorted output of <strong>all</strong> values as <strong>strings</strong>:</p> <pre><code>SELECT regexp_split_to_table(replace(replace(replace( col1 ,'chrx', 'chr22') ,'chry', 'chr23') ,'chrmt', 'chr24') , ',') AS col1 FROM tbl ORDER BY 1 </code></pre> <p><code>chr6_ux9</code> comes after <code>chr6</code> automatically in this scenario.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload