Sorting features in legend

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I have a map with a polygon layer containing 100+ features.



I want them to display on the legend and the Layer Tree using the Polygon's ID and then its name i.e: "Id - Name".



I'm using the following steps in QGIS:



Symbology -> Categorized -> concat(Id, ' - ', Name). 


However, after following the above steps they are displayed in the Legend and Layer Tree in this order :



1 - Name1
10 - Name10
100 - Name100
101 - Name101
11 - Name11
12 - Name12....
2 - Name2
20 - Name20
3 - Name3


So I read that it's sorting them as strings, and I want to know if there is a way to have them sorted by Id while still display like "ID - Name".







share|improve this question




























    up vote
    2
    down vote

    favorite












    I have a map with a polygon layer containing 100+ features.



    I want them to display on the legend and the Layer Tree using the Polygon's ID and then its name i.e: "Id - Name".



    I'm using the following steps in QGIS:



    Symbology -> Categorized -> concat(Id, ' - ', Name). 


    However, after following the above steps they are displayed in the Legend and Layer Tree in this order :



    1 - Name1
    10 - Name10
    100 - Name100
    101 - Name101
    11 - Name11
    12 - Name12....
    2 - Name2
    20 - Name20
    3 - Name3


    So I read that it's sorting them as strings, and I want to know if there is a way to have them sorted by Id while still display like "ID - Name".







    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have a map with a polygon layer containing 100+ features.



      I want them to display on the legend and the Layer Tree using the Polygon's ID and then its name i.e: "Id - Name".



      I'm using the following steps in QGIS:



      Symbology -> Categorized -> concat(Id, ' - ', Name). 


      However, after following the above steps they are displayed in the Legend and Layer Tree in this order :



      1 - Name1
      10 - Name10
      100 - Name100
      101 - Name101
      11 - Name11
      12 - Name12....
      2 - Name2
      20 - Name20
      3 - Name3


      So I read that it's sorting them as strings, and I want to know if there is a way to have them sorted by Id while still display like "ID - Name".







      share|improve this question














      I have a map with a polygon layer containing 100+ features.



      I want them to display on the legend and the Layer Tree using the Polygon's ID and then its name i.e: "Id - Name".



      I'm using the following steps in QGIS:



      Symbology -> Categorized -> concat(Id, ' - ', Name). 


      However, after following the above steps they are displayed in the Legend and Layer Tree in this order :



      1 - Name1
      10 - Name10
      100 - Name100
      101 - Name101
      11 - Name11
      12 - Name12....
      2 - Name2
      20 - Name20
      3 - Name3


      So I read that it's sorting them as strings, and I want to know if there is a way to have them sorted by Id while still display like "ID - Name".









      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 3 at 1:37









      Vince

      14k32444




      14k32444










      asked Sep 2 at 23:20









      Mouad

      111




      111




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote













          You can use lpad() function to add zeros (0) on the left side of the ID numbers based on the number of digits that cover the maximum ID number. For example, if the maximum number of digits of the ID number is 3, as in your case (100), then you need to write the following:



          concat(lpad("ID",3,'0'),' - ',"Name" )


          In this case the legend will be sorted correctly.



          In the following example, I have to convert the NEW_ID field from numerical to text, therefore, I used to_string() function:



          concat(lpad(to_string("NEW_ID"),3,'0'),' - ',"Name" )


          Here is the output:



          Output



          It is also sorted correctly in print composer:



          Output_in_Print_Composer






          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "79"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f294745%2fsorting-features-in-legend%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote













            You can use lpad() function to add zeros (0) on the left side of the ID numbers based on the number of digits that cover the maximum ID number. For example, if the maximum number of digits of the ID number is 3, as in your case (100), then you need to write the following:



            concat(lpad("ID",3,'0'),' - ',"Name" )


            In this case the legend will be sorted correctly.



            In the following example, I have to convert the NEW_ID field from numerical to text, therefore, I used to_string() function:



            concat(lpad(to_string("NEW_ID"),3,'0'),' - ',"Name" )


            Here is the output:



            Output



            It is also sorted correctly in print composer:



            Output_in_Print_Composer






            share|improve this answer


























              up vote
              5
              down vote













              You can use lpad() function to add zeros (0) on the left side of the ID numbers based on the number of digits that cover the maximum ID number. For example, if the maximum number of digits of the ID number is 3, as in your case (100), then you need to write the following:



              concat(lpad("ID",3,'0'),' - ',"Name" )


              In this case the legend will be sorted correctly.



              In the following example, I have to convert the NEW_ID field from numerical to text, therefore, I used to_string() function:



              concat(lpad(to_string("NEW_ID"),3,'0'),' - ',"Name" )


              Here is the output:



              Output



              It is also sorted correctly in print composer:



              Output_in_Print_Composer






              share|improve this answer
























                up vote
                5
                down vote










                up vote
                5
                down vote









                You can use lpad() function to add zeros (0) on the left side of the ID numbers based on the number of digits that cover the maximum ID number. For example, if the maximum number of digits of the ID number is 3, as in your case (100), then you need to write the following:



                concat(lpad("ID",3,'0'),' - ',"Name" )


                In this case the legend will be sorted correctly.



                In the following example, I have to convert the NEW_ID field from numerical to text, therefore, I used to_string() function:



                concat(lpad(to_string("NEW_ID"),3,'0'),' - ',"Name" )


                Here is the output:



                Output



                It is also sorted correctly in print composer:



                Output_in_Print_Composer






                share|improve this answer














                You can use lpad() function to add zeros (0) on the left side of the ID numbers based on the number of digits that cover the maximum ID number. For example, if the maximum number of digits of the ID number is 3, as in your case (100), then you need to write the following:



                concat(lpad("ID",3,'0'),' - ',"Name" )


                In this case the legend will be sorted correctly.



                In the following example, I have to convert the NEW_ID field from numerical to text, therefore, I used to_string() function:



                concat(lpad(to_string("NEW_ID"),3,'0'),' - ',"Name" )


                Here is the output:



                Output



                It is also sorted correctly in print composer:



                Output_in_Print_Composer







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 3 at 5:08

























                answered Sep 3 at 1:29









                ahmadhanb

                18.7k21543




                18.7k21543



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f294745%2fsorting-features-in-legend%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    Installing NextGIS Connect into QGIS 3?

                    One-line joke