End sort i.e. sort by ending of a word

End sort i.e. sort by ending of a word

24
Basic UserBasic User
24

    Oct 07, 2013#1

    Hello,
    I am trying to sort a file in Urdu by the character by which it ends. Each word is on a separate line
    An example in English would help:

    Code: Select all

    lychee
    fruit
    banana
    apple
    pear
    cherry
    
    I need the sort to be on the last letter of the word and then sorted recursively in reverse order

    Code: Select all

    banana
    lychee [i]This comes before apple[/i]
    apple
    pear
    fruit
    cherry
    
    A macro or a script would do just great.
    Just in case someone wants to work with live data, here is a small sample:

    Code: Select all

    ببا
    ببجا
    ببچو
    ببد
    ببر
    ببڑ
    ببز
    ببستگيا
    ببلا
    ببلو
    ببلے
    ببلی
    ببند
    ببنسلکلگے
    ببنسلکلئ
    ببهنو
    ببہتر
    ببھا
    ببھر
    ببھی
    ببو
    ببيا
    ببيشتر
    ببے
    ببی
    بپا
    بپتا
    بپتسما
    بپد
    بپر
    بپفسر
    بپلی
    بپھر
    بپو
    بتا
    بتبا
    بتتر
    بتتے
    بتتی
    بتجد
    بتجو
    بتحقی
    بتخے
    بتد
    بتذ
    بتر
    بتز
    بتستا
    بتصحی
    بتصر
    بتطا
    بتعثنا
    
    Many thanks

    6,603548
    Grand MasterGrand Master
    6,603548

      Oct 07, 2013#2

      Here is a macro solution which I have tested only on English example. It works hopefully also on Urdu words.

      Code: Select all

      InsertMode
      ColumnModeOff
      HexOff
      InsertMode
      ColumnModeOff
      HexOff
      PerlReOn
      Top
      Find MatchCase RegExp "^(.)"
      Replace All "\1#"
      Loop 0
      Find MatchCase RegExp "^(.+#)(.)"
      Replace All "\2\1"
      IfNotFound
      ExitLoop
      EndIf
      EndLoop
      Find MatchCase "#"
      Replace All ""
      SortAsc IgnoreCase 1 -1 0 0 0 0 0 0
      Find MatchCase RegExp "^(.)"
      Replace All "\1#"
      Loop 0
      Find MatchCase RegExp "^(.+#)(.)"
      Replace All "\2\1"
      IfNotFound
      ExitLoop
      EndIf
      EndLoop
      Find MatchCase "#"
      Replace All ""
      The macro reverses first all characters within a line. So for example fruit becomes tiurf. Then the lines are sorted ascending. And finally the macro reverses all characters within a line again. # is used temporarily on reversing the characters. This character should be not present in the file or the macro produces wrong output.

      24
      Basic UserBasic User
      24

        Oct 08, 2013#3

        Many thanks. The macro worked like a charm.