Community
    • Login

    Sort lines but keep the hierarchy

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 3 Posters 415 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Rune EgenesR
      Rune Egenes
      last edited by

      Hi,

      I hope someone can help me with this. I’m looking for a way for this:

      People
      –>Man
      –>Woman
      –>Child
      Sports
      –> Football
      –>Tennis
      –>Golf

      To sort and become like this:

      Sports
      –> Football
      –>Tennis
      –>Golf
      People
      –>Man
      –>Woman
      –>Child

      Thank you.

      1 Reply Last reply Reply Quote 0
      • guy038G
        guy038
        last edited by guy038

        Hello, @rune-egenes and All,

        Well, not difficult to achieve with regular expressions !


        So we start with this INPUT text :

        People
        –>Man
        –>Woman
        –>Child
        Sports
        –> Football
        –>Tennis
        –>Golf
        
        • Firstly, perform the following regex S/R, with displays all header strings in consecutive lines :

          • SEARCH \R(?=\x{2013}>)

          • REPLACE Leave EMPTY

        And we get the temporary text, below :

        People–>Man–>Woman–>Child
        Sports–> Football–>Tennis–>Golf
        
        • Secondly, run the option Edit > Line Operations > Sort Lines Lexicographically Descending

        After the sort operation, we get :

        Sports–> Football–>Tennis–>Golf
        People–>Man–>Woman–>Child
        
        • Thirdly, execute this last regex S/R, below :

          • SEARCH (?=\x{2013}>)

          • REPLACE \r\n    or    \n if you deal with UNIX files

        And, here is your expected OUTPUT text :

        Sports
        –> Football
        –>Tennis
        –>Golf
        People
        –>Man
        –>Woman
        –>Child
        

        Best Regards

        guy038

        Rune EgenesR 1 Reply Last reply Reply Quote 3
        • Rune EgenesR
          Rune Egenes @guy038
          last edited by

          @guy038 Thank you so much. I also have some lists that need to be sorted where
          -> is tab (\t) is there a regex for that? Thank you

          1 Reply Last reply Reply Quote 0
          • guy038G
            guy038
            last edited by

            Hi, @rune-egenes and All,

            Easy !

            • First regex :

              • SEARCH \R(?=\t)

              • REPLACE Leave EMPTY

            • Sort

            • Second regex

              • SEARCH (?=\t)

              • REPLACE \r\n    or    \n if you deal with UNIX files

            BR

            guy038

            1 Reply Last reply Reply Quote 1
            • Terry RT Terry R referenced this topic on
            • Mr. JaimsM
              Mr. Jaims
              last edited by

              This post is deleted!
              1 Reply Last reply Reply Quote -1
              • First post
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors