Page 1 of 1

Sorting alphabetically line by line

Posted: Tue Mar 16, 2010 11:09 pm
by simoninsing
Is there a function in TP that sorts text alphabetically line by line ?

Example:
apple, pear, tamarillo, orange, banana, "custard apple"
pear, tamarillo, orange, grapefruit, "custard apple", apple
tamarillo, orange, durian, "custard apple", apple, pear


becomes

apple, banana, "custard apple", orange, pear, tamarillo
apple, "custard apple", grapefruit, pear, tamarillo
apple, "custard apple", durian, orange, pear, tamarillo

Re: Sorting alphabetically line by line

Posted: Wed Mar 17, 2010 5:47 am
by DataMystic Support
You can achieve this by restricting to a line at a time, breaking fields onto separate lines, sorting, and then rejoining the lines.

See attached.

Re: Sorting alphabetically line by line

Posted: Wed Mar 17, 2010 9:04 am
by simoninsing
Thanks. I can't import the .fll file you attached (says it comes from a later version of TP, although I appear to have the latest version [8.4.9.1] so maybe it's because I have Lite not Standard/Pro) but reading up on Restrictions now.

Re: Sorting alphabetically line by line

Posted: Wed Mar 17, 2010 10:45 am
by AnthonyFitz
Hi,

I have created a filter that will do this also using the restrict to each line in turn filter. I tested it on the following input, and got the following output.

Input
apple, pear, tamarillo, orange, banana, "custard apple"
pear, tamarillo, orange, grapefruit, "custard apple", apple
tamarillo, orange, durian, "custard apple", apple, pear
Output
apple, banana, "custard apple", orange, pear, tamarillo
apple, "custard apple", grapefruit, orange, pear, tamarillo
apple, "custard apple", durian, orange, pear, tamarillo

Cheers,
Anthony.

Filter Text Export

Code: Select all

|--Restrict to each line in turn
|  |
|  |--Perl pattern ["] with []
|  |     [ ] Match case
|  |     [ ] Whole words only
|  |     [ ] Case sensitive replace
|  |     [ ] Prompt on replace
|  |     [ ] Skip prompt if identical
|  |     [ ] First only
|  |     [ ] Extract matches
|  |     Maximum text buffer size 4096
|  |     [ ] Maximum match (greedy)
|  |     [ ] Allow comments
|  |     [X] '.' matches newline
|  |     [ ] UTF-8 Support
|  |   
|  |--Perl pattern [, ??] with [\r\n]
|  |     [ ] Match case
|  |     [ ] Whole words only
|  |     [ ] Case sensitive replace
|  |     [ ] Prompt on replace
|  |     [ ] Skip prompt if identical
|  |     [ ] First only
|  |     [ ] Extract matches
|  |     Maximum text buffer size 4096
|  |     [ ] Maximum match (greedy)
|  |     [ ] Allow comments
|  |     [X] '.' matches newline
|  |     [ ] UTF-8 Support
|  |   
|  |--Remove blanks from Start of Line
|  |   
|  |--Remove blanks from End of Line
|  |   
|  |--Ascending ANSI sort (case insensitive)
|  |   
|  |--Perl pattern [^(.* .*)\r\n] with ["$1"\r\n]
|  |     [ ] Match case
|  |     [ ] Whole words only
|  |     [ ] Case sensitive replace
|  |     [ ] Prompt on replace
|  |     [ ] Skip prompt if identical
|  |     [ ] First only
|  |     [ ] Extract matches
|  |     Maximum text buffer size 4096
|  |     [ ] Maximum match (greedy)
|  |     [ ] Allow comments
|  |     [ ] '.' matches newline
|  |     [ ] UTF-8 Support
|  |   
|  +--Perl pattern [\r\n(?!\Z)] with [, ]
|        [ ] Match case
|        [ ] Whole words only
|        [ ] Case sensitive replace
|        [ ] Prompt on replace
|        [ ] Skip prompt if identical
|        [ ] First only
|        [ ] Extract matches
|        Maximum text buffer size 4096
|        [ ] Maximum match (greedy)
|        [ ] Allow comments
|        [X] '.' matches newline
|        [ ] UTF-8 Support

Re: Sorting alphabetically line by line

Posted: Wed Mar 17, 2010 11:02 am
by simoninsing
Many thanks Anthony. It looks great. I'll need to upgrade to Pro to use it, which I'll do soon ... as soon as I feel rich enough !

Re: Sorting alphabetically line by line

Posted: Wed Mar 17, 2010 12:08 pm
by DataMystic Support
Sorry, I forgot I was using the 8.5 pre-release!

Code: Select all

|   
|--Perl pattern [, ] with [,]
|     [ ] Match case
|     [ ] Whole words only
|     [ ] Case sensitive replace
|     [ ] Prompt on replace
|     [ ] Skip prompt if identical
|     [ ] First only
|     [ ] Extract matches
|     Maximum text buffer size 4096
|     [ ] Maximum match (greedy)
|     [ ] Allow comments
|     [X] '.' matches newline
|     [ ] UTF-8 Support
|   
|--Restrict to each line in turn
|  |
|  |--EasyPattern [[ csvfield ]] with [$0\r\n]
|  |     [ ] Match case
|  |     [ ] Whole words only
|  |     [ ] Case sensitive replace
|  |     [ ] Prompt on replace
|  |     [ ] Skip prompt if identical
|  |     [ ] First only
|  |     [X] Extract matches
|  |     Maximum text buffer size 4096
|  |   
|  |--Ascending ANSI sort (case insensitive), length 50
|  |   
|  |--Perl pattern [\r\n] with [,]
|  |     [ ] Match case
|  |     [ ] Whole words only
|  |     [ ] Case sensitive replace
|  |     [ ] Prompt on replace
|  |     [ ] Skip prompt if identical
|  |     [ ] First only
|  |     [ ] Extract matches
|  |     Maximum text buffer size 4096
|  |     [X] Maximum match (greedy)
|  |     [ ] Allow comments
|  |     [X] '.' matches newline
|  |     [ ] UTF-8 Support
|  |   
|  +--Add footer [\r\n]
|      

Re: Sorting alphabetically line by line

Posted: Thu Mar 18, 2010 7:54 am
by AnthonyFitz
simoninsing wrote:Many thanks Anthony. It looks great. I'll need to upgrade to Pro to use it, which I'll do soon ... as soon as I feel rich enough !
Best purchase you'll ever make. You wont regret it!