Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: pl/sql - sorting a string
Here's an idea for a rough algorithm. Extract a token (state), if its less
than the previous token remove it from the string and concatenate it to the
front. Reset your scan to the beginning of the string. Continue until you
can get through the whole string.
Example:
'TX LA KS SD MD' start 'LA TX KS SD MD' one pass, LA removed and put in front 'KS LA TX SD MD' 'SD KS LA TX MD' 'KS SD LA TX MD' 'LA KS SD TX MD' 'KS LA SD TX MD' 'MD KS LA SD TX' 'KS MD LA SD TX' 'LA KS MD SD TX' 'KS LA MD SD TX' done
Not the most efficient perhaps, but easy to code. You might try changing the algorithm to always scan to the end of the string. This may reduce the iterations and in the case where all the tokens are in descending (reverse) order it will sort in one pass!
Mark Aengenheyster wrote:
> I need to build a PL/SQL function that does the following:
>
> Take a 'space' delimited string of state abbrev's : 'TX LA KS SD MD'
> and put it in alphabetical order: 'KS LA MD SD TX'
> I already wrote a loop that extracts the individual states into a string
> variable for each iteration of the loop.
>
> Should I put them into a TABLE variable (an array) and then sort it or
> what?
> Does anyone know where I can get an example of sorting a TABLE variable
> in PL/SQL (sorting an array)?
> Or should I do it a different way?
>
> Thanks in advance - Mark
Received on Mon Jun 29 1998 - 23:07:47 CDT