Sorting strings in reverse order

Hello,
I have a large database of words and would like them sorted in reverse order i.e. from the end up.
An example will make this clear:

I have tried to write a program in Perl which basically takes the string from the end and tries to sort from that end but it does not seem to work. I also tried to invoke the scalar reverse function but this does not work.
The data is in an Indic language and is huge: around 300,000 strings.
Any help in the script would be highly appreciated.

How about this :

Perl solution :

1 Like

Many thanks will check and get back to you.

With perl (without invoking the interpreter twice and without calling any external utility but memory-intensive depending on the file size):

perl -lne 'push @strs,$_;END{print for map {scalar reverse} sort map {scalar reverse} @strs}' file
1 Like

Many thanks. Am out and don't have a perl compiler to check the code, but will definitely get back to you with the results.
Thanks for all your help

---------- Post updated at 06:58 AM ---------- Previous update was at 04:14 AM ----------

Many thanks to both. The two solutions worked beautifully. The one proposed by elixir_sinari is faster and as he himself states
With perl (without invoking the interpreter twice and without calling any external utility):
Thanks for the quick postings and the useful code. I learned finally how to handle scalar reverse, which is a great learning experience for me.

1 Like

Hi.

Among the many additional features of utility msort compared to standard sort is the ability to specify a reverse sequence of keys:

#!/usr/bin/env bash

# @(#) s1	Demonstrate reversed character key sequencing, msort.
# See: http://freecode.com/projects/msort

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C msort

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results, normal key sequencing:"
msort -q --line -n 1,1 $FILE

pl " Results, reversed key sequencing:"
msort -q --line -n 1,1 -R $FILE

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
msort 8.44

-----
 Input data file data1:
John
Mary
Henry
does
doing
writes

-----
 Results, normal key sequencing:
Henry
John
Mary
does
doing
writes

-----
 Results, reversed key sequencing:
doing
John
does
writes
Mary
Henry

The msort code was in the Debian repository (as well as Fedora, Ubuntu, and FreeBSD). See site mentioned in script for others.

Best wishes ... cheers, drl