7 Replies Latest reply on Feb 2, 2017 3:12 AM by Obi-wan Kenobi

    Code to be evaluated! [014] Sorting Numbers + Text! …

    Obi-wan Kenobi Adobe Community Professional

      Hi Scripters,

       

      Bored by this code:

       

       

      var myDoc = app.activeDocument,
      mySel = myDoc.selection[0];;
      
      
      app.findGrepPreferences = app.changeGrepPreferences = null;
      app.findGrepPreferences.findWhat = "\\d[\\d\\h,@#]+";
      myFound = mySel.findGrep();
      var F = myFound.length;
      for ( var f = 0 ; f < F ; f++) {
              myNewFound = myFound[f].contents.split(", ").sort( function (a,b) { return Number (a) > Number (b) } ).join(", ");
              myFound[f].contents = myNewFound;
              }
      app.findGrepPreferences = app.changeGrepPreferences = null;
      

       

       

      Capture d’écran 2017-02-01 à 17.22.26.png

       

      First case: what I have at the beginning

      Second case: what I get with this code

      Third case: what I would like to have!

       

      I suppose my problem could be about the line 10 but I don't find a fonction definition to sort as number and as text!

       

      Thanks in advance!

       

      (^/)

        • 1. Re: Code to be evaluated! [014] Sorting Numbers + Text! …
          Laubender Adobe Community Professional & MVP

          Hi Obi-wan,

           

          could you weight perhaps "@" and "#" in the order you like to sort them?

           

          Something like that perhaps?

           

          var a1 = ["2","5","12","18","30","2@","3@","19@","2#","3#","18#"];
          
          for(var n=0;n<a1.length;n++)
          {
              a1[n] = Number
              (
                  a1[n].
                  replace(/@$/,".1").
                  replace(/#$/,".2")
              );
          };
          
          a1.sort( function(a,b){return Number (a) > Number (b)});
          
          for(var n=0;n<a1.length;n++)
          {
              a1[n] = a1[n].toString().
              replace(/\.1$/,"@").
              replace(/\.2$/,"#");
          };
          
          a1;
          

           

          Regards,
          Uwe

          • 2. Re: Code to be evaluated! [014] Sorting Numbers + Text! …
            Obi-wan Kenobi Adobe Community Professional

            Hi Uwe,

             

            Clever! Thanks! [I take it!] 

            I've used a similar way:

            Add 3 zeros to all numbers, except those that finish by "@" and "#", replaced by, respectively, "001" and "002" (and inverse the process later).

            The interest is that I can choose to add "001" to all, except those that finish by "@" and "#", replaced by, respectively, "000" and "002"

            ==> to sort them differently!

             

            … But I thought [JS] could sort the context I talk about without these turn-around!

            First, numbers as "Number()", then last glyph as "String()".

             

            (^/)

            • 3. Re: Code to be evaluated! [014] Sorting Numbers + Text! …
              Ronald63 Level 4

              Hello,

               

              Another solution ...

              http://stackoverflow.com/questions/15478954/sort-array-elements-string-with-numbers-natura l-sort

              var a1 = ["2","5","12","18","30","2@","3@","19@","2#","3#","18#"]; 
              
              a1.sort(naturalCompare)
              
              alert(a1)
              
              function naturalCompare(a, b) {
                  var ax = [], bx = [];
              
                  a.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { ax.push([$1 || Infinity, $2 || ""]) });
                  b.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { bx.push([$1 || Infinity, $2 || ""]) });
                  
                  while(ax.length && bx.length) {
                      var an = ax.shift();
                      var bn = bx.shift();
                      var nn = (an[0] - bn[0]) || an[1].localeCompare(bn[1]);
                      if(nn) return nn;
                  }
              
                  return ax.length - bx.length;
              }
              
              • 4. Re: Code to be evaluated! [014] Sorting Numbers + Text! …
                [Jongware] Most Valuable Participant

                It's easy! You only need one single custom sort function!

                 

                before: 2,5,12,18,30,2@,3@,19@,2#,3#,18#

                after: 2,2@,2#,3@,3#,5,12,18,18#,19@,30

                • 5. Re: Code to be evaluated! [014] Sorting Numbers + Text! …
                  Obi-wan Kenobi Adobe Community Professional

                  Thanks Ronald! I'm going to test it!

                   

                  Hi Jongware! What do you mean?    [It's always easy for you! … Alas, not for me!  Aha!]

                   

                  (^/)

                  • 6. Re: Code to be evaluated! [014] Sorting Numbers + Text! …
                    [Jongware] Most Valuable Participant
                    var a1 = ["2","5","12","18","30","2@","3@","19@","2#","3#","18#"]; 
                    var a2 = a1.slice();
                    
                    a2.sort (function(a,b){
                     aa = Number(a.match(/\d+/));
                     bb = Number(b.match(/\d+/));
                     if (aa != bb)
                      return aa - bb;
                     aa = [a.match(/\D+/)].join().replace('@','1').replace('#','2');
                     bb = [b.match(/\D+/)].join().replace('@','1').replace('#','2');
                     return aa < bb;
                    });
                    
                    alert ('before: '+a1+'\rafter: '+a2);
                    

                     

                    (opps, lexicographic sorting does not work with your atypical sorting of '@' before '#' instead of after. Now this also works with "2a" and "2b"!)

                    • 7. Re: Code to be evaluated! [014] Sorting Numbers + Text! …
                      Obi-wan Kenobi Adobe Community Professional

                      Thanks Jongware!

                       

                      Great comments of you 3! Very cool for my "Sorting" Javascript code learning!

                      I'm going to study all your approaches! [maybe surely some others questions from me!]

                       

                      About the "@" and "#" use, it's an "only temporary" use in my process. Surely an error from me to use them [a & b could be better]!

                       

                      The deal is: In an "Index" context, some entries are more important than others, some specific (less important than "basic" entries). So I thought about applying "Bold" to the first group page numbers (here, + blue color to make them more visible) and "Italic" (+ red color) for the second group! As, on the left, after Index génération, on the right, after script.

                       

                      Capture d’écran 2017-02-02 à 11.55.45.png

                       

                      It's done with my own approach, but I'm really more interested by yours (x3)! 

                       

                      Thanks a lot for your comments!

                       

                      (^/)