Sorting a clist in LaTeX3












9














I have a clist that I'd like to bubble sort. I'm sure this is a simple matter of something like clist_bubblesort:N or something, but I don't know what the appropriate incantation is. A quick google didn't reveal anything obvious...










share|improve this question



























    9














    I have a clist that I'd like to bubble sort. I'm sure this is a simple matter of something like clist_bubblesort:N or something, but I don't know what the appropriate incantation is. A quick google didn't reveal anything obvious...










    share|improve this question

























      9












      9








      9







      I have a clist that I'd like to bubble sort. I'm sure this is a simple matter of something like clist_bubblesort:N or something, but I don't know what the appropriate incantation is. A quick google didn't reveal anything obvious...










      share|improve this question













      I have a clist that I'd like to bubble sort. I'm sure this is a simple matter of something like clist_bubblesort:N or something, but I don't know what the appropriate incantation is. A quick google didn't reveal anything obvious...







      sorting latex3 expl3






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 8 '13 at 12:14









      Seamus

      44.9k35215332




      44.9k35215332






















          1 Answer
          1






          active

          oldest

          votes


















          13














          The l3sort package does that (but currently with a merge sort, I hope you don't mind the speed-up), as long as the clist has at most 20000 items or so (I can't remember the exact limit).



          documentclass{article}
          usepackage{expl3, xparse}
          ExplSyntaxOn
          clist_new:N l_my_clist
          NewDocumentCommand{sorted}{m}
          {
          clist_set:Nn l_my_clist {#1}
          clist_sort:Nn l_my_clist
          { int_compare:nTF { ##1 > ##2 } { sort_return_swapped: } { sort_return_same: } }
          clist_use:Nnnn l_my_clist { ~ and ~ } { , ~ } { , ~ and ~ }
          }
          ExplSyntaxOff
          begin{document}
          The sorted numbers are sorted{3, 4, 123, -1, 2, -01}.
          end{document}


          The command stores the given list in a clist variable, l_my_clist, then sorts it, using the following criterion: compare two items as integers; if the first is greater than the second, they must be swapped; otherwise keep them in the same order. Then use that clist, with the appropriate separators: ~and~ for lists with two items, ,~ between most items, ending with ,~and~. The ~ are in fact space tokens since space is ignored.



          Note that the sort is stable, in the sense that numbers that compare equal are considered ordered (this relies, of course, on choosing the test appropriately, calling sort_return_same: when the numbers are equal, as is done here).



          I have a longer term plan to provide something like sort_keep_one:n {##1} which would allow to keep only one of the two terms, which is useful to remove duplicates. I can imagine this being useful too when building an index, keeping only one copy of each work, but keeping track of a list of page numbers: sort_keep_one:n { ##1 ~ <pages> }.






          share|improve this answer



















          • 4




            We should probably finalise this module, if we can: it's a useful and general idea, needing only a good interface discussion.
            – Joseph Wright
            May 8 '13 at 12:47










          • @JosephWright I agree. On my side I need to unearth some code that would let us have this sort_keep_one:n functionality, as the current approach with merge sort makes it impossible.
            – Bruno Le Floch
            May 8 '13 at 12:59










          • That's an implementation thing, right? If so, we should take about interface on the LaTeX-L list and get that sorted ;-)
            – Joseph Wright
            May 8 '13 at 13:08










          • Yes, but it is important to know what is feasible/not (so my main point was "there exists an efficient implementation where it is possible to reduce the number of items in the list"). Let's.
            – Bruno Le Floch
            May 8 '13 at 13:50






          • 3




            Maybe this answer should be updated to the current state of expl3 (other commands for returning the state, not experimental, but in official interface3 and maybe an update on the sort_keep_one stuff).
            – TeXnician
            Apr 5 at 8:47













          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "85"
          };
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f113206%2fsorting-a-clist-in-latex3%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          13














          The l3sort package does that (but currently with a merge sort, I hope you don't mind the speed-up), as long as the clist has at most 20000 items or so (I can't remember the exact limit).



          documentclass{article}
          usepackage{expl3, xparse}
          ExplSyntaxOn
          clist_new:N l_my_clist
          NewDocumentCommand{sorted}{m}
          {
          clist_set:Nn l_my_clist {#1}
          clist_sort:Nn l_my_clist
          { int_compare:nTF { ##1 > ##2 } { sort_return_swapped: } { sort_return_same: } }
          clist_use:Nnnn l_my_clist { ~ and ~ } { , ~ } { , ~ and ~ }
          }
          ExplSyntaxOff
          begin{document}
          The sorted numbers are sorted{3, 4, 123, -1, 2, -01}.
          end{document}


          The command stores the given list in a clist variable, l_my_clist, then sorts it, using the following criterion: compare two items as integers; if the first is greater than the second, they must be swapped; otherwise keep them in the same order. Then use that clist, with the appropriate separators: ~and~ for lists with two items, ,~ between most items, ending with ,~and~. The ~ are in fact space tokens since space is ignored.



          Note that the sort is stable, in the sense that numbers that compare equal are considered ordered (this relies, of course, on choosing the test appropriately, calling sort_return_same: when the numbers are equal, as is done here).



          I have a longer term plan to provide something like sort_keep_one:n {##1} which would allow to keep only one of the two terms, which is useful to remove duplicates. I can imagine this being useful too when building an index, keeping only one copy of each work, but keeping track of a list of page numbers: sort_keep_one:n { ##1 ~ <pages> }.






          share|improve this answer



















          • 4




            We should probably finalise this module, if we can: it's a useful and general idea, needing only a good interface discussion.
            – Joseph Wright
            May 8 '13 at 12:47










          • @JosephWright I agree. On my side I need to unearth some code that would let us have this sort_keep_one:n functionality, as the current approach with merge sort makes it impossible.
            – Bruno Le Floch
            May 8 '13 at 12:59










          • That's an implementation thing, right? If so, we should take about interface on the LaTeX-L list and get that sorted ;-)
            – Joseph Wright
            May 8 '13 at 13:08










          • Yes, but it is important to know what is feasible/not (so my main point was "there exists an efficient implementation where it is possible to reduce the number of items in the list"). Let's.
            – Bruno Le Floch
            May 8 '13 at 13:50






          • 3




            Maybe this answer should be updated to the current state of expl3 (other commands for returning the state, not experimental, but in official interface3 and maybe an update on the sort_keep_one stuff).
            – TeXnician
            Apr 5 at 8:47


















          13














          The l3sort package does that (but currently with a merge sort, I hope you don't mind the speed-up), as long as the clist has at most 20000 items or so (I can't remember the exact limit).



          documentclass{article}
          usepackage{expl3, xparse}
          ExplSyntaxOn
          clist_new:N l_my_clist
          NewDocumentCommand{sorted}{m}
          {
          clist_set:Nn l_my_clist {#1}
          clist_sort:Nn l_my_clist
          { int_compare:nTF { ##1 > ##2 } { sort_return_swapped: } { sort_return_same: } }
          clist_use:Nnnn l_my_clist { ~ and ~ } { , ~ } { , ~ and ~ }
          }
          ExplSyntaxOff
          begin{document}
          The sorted numbers are sorted{3, 4, 123, -1, 2, -01}.
          end{document}


          The command stores the given list in a clist variable, l_my_clist, then sorts it, using the following criterion: compare two items as integers; if the first is greater than the second, they must be swapped; otherwise keep them in the same order. Then use that clist, with the appropriate separators: ~and~ for lists with two items, ,~ between most items, ending with ,~and~. The ~ are in fact space tokens since space is ignored.



          Note that the sort is stable, in the sense that numbers that compare equal are considered ordered (this relies, of course, on choosing the test appropriately, calling sort_return_same: when the numbers are equal, as is done here).



          I have a longer term plan to provide something like sort_keep_one:n {##1} which would allow to keep only one of the two terms, which is useful to remove duplicates. I can imagine this being useful too when building an index, keeping only one copy of each work, but keeping track of a list of page numbers: sort_keep_one:n { ##1 ~ <pages> }.






          share|improve this answer



















          • 4




            We should probably finalise this module, if we can: it's a useful and general idea, needing only a good interface discussion.
            – Joseph Wright
            May 8 '13 at 12:47










          • @JosephWright I agree. On my side I need to unearth some code that would let us have this sort_keep_one:n functionality, as the current approach with merge sort makes it impossible.
            – Bruno Le Floch
            May 8 '13 at 12:59










          • That's an implementation thing, right? If so, we should take about interface on the LaTeX-L list and get that sorted ;-)
            – Joseph Wright
            May 8 '13 at 13:08










          • Yes, but it is important to know what is feasible/not (so my main point was "there exists an efficient implementation where it is possible to reduce the number of items in the list"). Let's.
            – Bruno Le Floch
            May 8 '13 at 13:50






          • 3




            Maybe this answer should be updated to the current state of expl3 (other commands for returning the state, not experimental, but in official interface3 and maybe an update on the sort_keep_one stuff).
            – TeXnician
            Apr 5 at 8:47
















          13












          13








          13






          The l3sort package does that (but currently with a merge sort, I hope you don't mind the speed-up), as long as the clist has at most 20000 items or so (I can't remember the exact limit).



          documentclass{article}
          usepackage{expl3, xparse}
          ExplSyntaxOn
          clist_new:N l_my_clist
          NewDocumentCommand{sorted}{m}
          {
          clist_set:Nn l_my_clist {#1}
          clist_sort:Nn l_my_clist
          { int_compare:nTF { ##1 > ##2 } { sort_return_swapped: } { sort_return_same: } }
          clist_use:Nnnn l_my_clist { ~ and ~ } { , ~ } { , ~ and ~ }
          }
          ExplSyntaxOff
          begin{document}
          The sorted numbers are sorted{3, 4, 123, -1, 2, -01}.
          end{document}


          The command stores the given list in a clist variable, l_my_clist, then sorts it, using the following criterion: compare two items as integers; if the first is greater than the second, they must be swapped; otherwise keep them in the same order. Then use that clist, with the appropriate separators: ~and~ for lists with two items, ,~ between most items, ending with ,~and~. The ~ are in fact space tokens since space is ignored.



          Note that the sort is stable, in the sense that numbers that compare equal are considered ordered (this relies, of course, on choosing the test appropriately, calling sort_return_same: when the numbers are equal, as is done here).



          I have a longer term plan to provide something like sort_keep_one:n {##1} which would allow to keep only one of the two terms, which is useful to remove duplicates. I can imagine this being useful too when building an index, keeping only one copy of each work, but keeping track of a list of page numbers: sort_keep_one:n { ##1 ~ <pages> }.






          share|improve this answer














          The l3sort package does that (but currently with a merge sort, I hope you don't mind the speed-up), as long as the clist has at most 20000 items or so (I can't remember the exact limit).



          documentclass{article}
          usepackage{expl3, xparse}
          ExplSyntaxOn
          clist_new:N l_my_clist
          NewDocumentCommand{sorted}{m}
          {
          clist_set:Nn l_my_clist {#1}
          clist_sort:Nn l_my_clist
          { int_compare:nTF { ##1 > ##2 } { sort_return_swapped: } { sort_return_same: } }
          clist_use:Nnnn l_my_clist { ~ and ~ } { , ~ } { , ~ and ~ }
          }
          ExplSyntaxOff
          begin{document}
          The sorted numbers are sorted{3, 4, 123, -1, 2, -01}.
          end{document}


          The command stores the given list in a clist variable, l_my_clist, then sorts it, using the following criterion: compare two items as integers; if the first is greater than the second, they must be swapped; otherwise keep them in the same order. Then use that clist, with the appropriate separators: ~and~ for lists with two items, ,~ between most items, ending with ,~and~. The ~ are in fact space tokens since space is ignored.



          Note that the sort is stable, in the sense that numbers that compare equal are considered ordered (this relies, of course, on choosing the test appropriately, calling sort_return_same: when the numbers are equal, as is done here).



          I have a longer term plan to provide something like sort_keep_one:n {##1} which would allow to keep only one of the two terms, which is useful to remove duplicates. I can imagine this being useful too when building an index, keeping only one copy of each work, but keeping track of a list of page numbers: sort_keep_one:n { ##1 ~ <pages> }.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 11 mins ago









          tambre

          1688




          1688










          answered May 8 '13 at 12:38









          Bruno Le Floch

          33.9k5114211




          33.9k5114211








          • 4




            We should probably finalise this module, if we can: it's a useful and general idea, needing only a good interface discussion.
            – Joseph Wright
            May 8 '13 at 12:47










          • @JosephWright I agree. On my side I need to unearth some code that would let us have this sort_keep_one:n functionality, as the current approach with merge sort makes it impossible.
            – Bruno Le Floch
            May 8 '13 at 12:59










          • That's an implementation thing, right? If so, we should take about interface on the LaTeX-L list and get that sorted ;-)
            – Joseph Wright
            May 8 '13 at 13:08










          • Yes, but it is important to know what is feasible/not (so my main point was "there exists an efficient implementation where it is possible to reduce the number of items in the list"). Let's.
            – Bruno Le Floch
            May 8 '13 at 13:50






          • 3




            Maybe this answer should be updated to the current state of expl3 (other commands for returning the state, not experimental, but in official interface3 and maybe an update on the sort_keep_one stuff).
            – TeXnician
            Apr 5 at 8:47
















          • 4




            We should probably finalise this module, if we can: it's a useful and general idea, needing only a good interface discussion.
            – Joseph Wright
            May 8 '13 at 12:47










          • @JosephWright I agree. On my side I need to unearth some code that would let us have this sort_keep_one:n functionality, as the current approach with merge sort makes it impossible.
            – Bruno Le Floch
            May 8 '13 at 12:59










          • That's an implementation thing, right? If so, we should take about interface on the LaTeX-L list and get that sorted ;-)
            – Joseph Wright
            May 8 '13 at 13:08










          • Yes, but it is important to know what is feasible/not (so my main point was "there exists an efficient implementation where it is possible to reduce the number of items in the list"). Let's.
            – Bruno Le Floch
            May 8 '13 at 13:50






          • 3




            Maybe this answer should be updated to the current state of expl3 (other commands for returning the state, not experimental, but in official interface3 and maybe an update on the sort_keep_one stuff).
            – TeXnician
            Apr 5 at 8:47










          4




          4




          We should probably finalise this module, if we can: it's a useful and general idea, needing only a good interface discussion.
          – Joseph Wright
          May 8 '13 at 12:47




          We should probably finalise this module, if we can: it's a useful and general idea, needing only a good interface discussion.
          – Joseph Wright
          May 8 '13 at 12:47












          @JosephWright I agree. On my side I need to unearth some code that would let us have this sort_keep_one:n functionality, as the current approach with merge sort makes it impossible.
          – Bruno Le Floch
          May 8 '13 at 12:59




          @JosephWright I agree. On my side I need to unearth some code that would let us have this sort_keep_one:n functionality, as the current approach with merge sort makes it impossible.
          – Bruno Le Floch
          May 8 '13 at 12:59












          That's an implementation thing, right? If so, we should take about interface on the LaTeX-L list and get that sorted ;-)
          – Joseph Wright
          May 8 '13 at 13:08




          That's an implementation thing, right? If so, we should take about interface on the LaTeX-L list and get that sorted ;-)
          – Joseph Wright
          May 8 '13 at 13:08












          Yes, but it is important to know what is feasible/not (so my main point was "there exists an efficient implementation where it is possible to reduce the number of items in the list"). Let's.
          – Bruno Le Floch
          May 8 '13 at 13:50




          Yes, but it is important to know what is feasible/not (so my main point was "there exists an efficient implementation where it is possible to reduce the number of items in the list"). Let's.
          – Bruno Le Floch
          May 8 '13 at 13:50




          3




          3




          Maybe this answer should be updated to the current state of expl3 (other commands for returning the state, not experimental, but in official interface3 and maybe an update on the sort_keep_one stuff).
          – TeXnician
          Apr 5 at 8:47






          Maybe this answer should be updated to the current state of expl3 (other commands for returning the state, not experimental, but in official interface3 and maybe an update on the sort_keep_one stuff).
          – TeXnician
          Apr 5 at 8:47




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f113206%2fsorting-a-clist-in-latex3%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          サソリ

          広島県道265号伴広島線

          Setup Asymptote in Texstudio