Postgresql函数对字符串中的字符进行排序 [英] Postgresql Function to sort characters within a string

查看:1974
本文介绍了Postgresql函数对字符串中的字符进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在一个postgresql函数,最好是本机函数,该函数可以将诸如'banana'的字符串排序为'aaabnn'?

Is there a postgresql function, preferably native function, that can sort a string such as 'banana' to 'aaabnn'?

排序的算法效率不是很重要,因为单词永远不会太长.但是,数据库联接效率具有某些但并非至关重要的重要性.

Algorithmic efficiency of sorting is not of much importance since words will never be too long. However, database join efficiency is of some but not critical importance.

推荐答案

没有本机功能具有这样的功能,但是您可以使用regexp_split_to_table来做到这一点:

There is no native function with such functionality but you can use regexp_split_to_table to do so as this:

select theword 
  from (select regexp_split_to_table('banana',E'(?=.)') theword) tab 
 order by theword;

结果将是:

theword
   a
   a
   a
   b
   n
   n

(?=.)将按每个字符分隔,将字符作为分隔符.它还将标识空格.如果您有带空格的单词,并且不希望使用空格(空格),请使用E'(\\s*)'匹配任何空白字符.我不记得E是什么意思.我将尽快搜索和编辑答案.

This (?=.) will split by each character leaving the character as separator. It will also identify spaces. If you have a word with spaces and do not want it (the space) use E'(\\s*)' matches any whitespace character. I don't recall what the E means. I will search and edit the answer asap.

DOCs所述所述a>在"regexp_split_to_table"部分

As explained in the DOCs in the section "regexp_split_to_table"

正如我所说:E在字符串前面的含义可以在这里看到:

As I said: The meaning of the E before the string you can see here: What's the "E" before a Postgres string?

这篇关于Postgresql函数对字符串中的字符进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆