sort()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    sort()

    sorry for the last post, itchy fingers.

    I'm having a bit of difficulty sorting images named in sequential numerical
    order. Here are the image names and how I need them sorted.

    image1.jpg
    image2.jpg
    image3.jpg
    image4.jpg
    image5.jpg
    image6.jpg
    image7.jpg
    image8.jpg
    image9.jpg
    image10.jpg
    image11.jpg
    image12.jpg

    Using array.sort() here is how it gets sorted,

    image1.jpg
    image10.jpg
    image11.jpg
    image12.jpg
    image2.jpg
    image3.jpg
    image4.jpg
    image5.jpg
    image6.jpg
    image7.jpg
    image8.jpg
    image9.jpg

    Any ideas how to correct this? I've tried a few methods unsuccssfully.

    David









  • Evertjan.

    #2
    Re: sort()

    David wrote on 04 mei 2005 in comp.lang.javas cript:
    [color=blue]
    > sorry for the last post, itchy fingers.
    >
    > I'm having a bit of difficulty sorting images named in sequential
    > numerical order. Here are the image names and how I need them sorted.
    >
    > image1.jpg
    > image2.jpg
    > image3.jpg
    > image4.jpg
    > image5.jpg
    > image6.jpg
    > image7.jpg
    > image8.jpg
    > image9.jpg
    > image10.jpg
    > image11.jpg
    > image12.jpg
    >
    > Using array.sort() here is how it gets sorted,
    >
    > image1.jpg
    > image10.jpg
    > image11.jpg
    > image12.jpg
    > image2.jpg
    > image3.jpg
    > image4.jpg
    > image5.jpg
    > image6.jpg
    > image7.jpg
    > image8.jpg
    > image9.jpg
    >
    > Any ideas how to correct this? I've tried a few methods unsuccssfully.[/color]

    Why, it is a corect result of an alphanumeric sort?

    [see my earlier posting]

    However, if you want a numerical sort:

    =============== ============

    s = "image4.jpg,ima ge9.jpg,image11 1.jpg,image2.jp g,image1.jpg"

    s = s.split(',').so rt(compare).joi n(',')

    document.write( s)

    function compare(a, b) {
    a = +a.replace(/image/g,'').replace(/\.jpg/g,'')
    b = +b.replace(/image/g,'').replace(/\.jpg/g,'')
    if (a<b) return -1
    if (a>b) return 1
    return 0
    }

    =============== =============

    this returns:

    image1.jpg,imag e2.jpg,image4.j pg,image9.jpg,i mage111.jpg

    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)

    Comment

    • Paul Cooper

      #3
      Re: sort()

      On Wed, 04 May 2005 15:52:32 GMT, "David" <right@dd.com > wrote:
      [color=blue]
      >sorry for the last post, itchy fingers.
      >
      >I'm having a bit of difficulty sorting images named in sequential numerical
      >order. Here are the image names and how I need them sorted.
      >
      >image1.jpg
      >image2.jpg
      >image3.jpg
      >image4.jpg
      >image5.jpg
      >image6.jpg
      >image7.jpg
      >image8.jpg
      >image9.jpg
      >image10.jpg
      >image11.jpg
      >image12.jpg
      >
      >Using array.sort() here is how it gets sorted,
      >
      >image1.jpg
      >image10.jpg
      >image11.jpg
      >image12.jpg
      >image2.jpg
      >image3.jpg
      >image4.jpg
      >image5.jpg
      >image6.jpg
      >image7.jpg
      >image8.jpg
      >image9.jpg
      >
      >Any ideas how to correct this? I've tried a few methods unsuccssfully.
      >
      >David
      >
      >
      >
      >
      >
      >
      >
      >[/color]

      For the names you have, this is the correct alphanumeric sort - the
      sort routine doesn't know to treat the digits at the end of the name
      as numbers. Rename your images so they always have the same number of
      digits in the name, and that will fix it (i.e. image01.jpg,
      image02.jpg...i mage09.jpg,imag e10.jpg).

      Of course, this will break if you use more than 99 images and you'd
      have to add another digit!

      Paul

      Comment

      • Evertjan.

        #4
        Re: sort()

        Paul Cooper wrote on 04 mei 2005 in comp.lang.javas cript:[color=blue]
        > For the names you have, this is the correct alphanumeric sort - the
        > sort routine doesn't know to treat the digits at the end of the name
        > as numbers.[/color]

        True, Paul, but it can learn, if you teach it.


        --
        Evertjan.
        The Netherlands.
        (Replace all crosses with dots in my emailaddress)

        Comment

        • RobB

          #5
          Re: sort()

          David wrote:[color=blue]
          > sorry for the last post, itchy fingers.
          >
          > I'm having a bit of difficulty sorting images named in sequential[/color]
          numerical[color=blue]
          > order. Here are the image names and how I need them sorted.
          >
          > image1.jpg
          > image2.jpg
          > image3.jpg
          > image4.jpg
          > image5.jpg
          > image6.jpg
          > image7.jpg
          > image8.jpg
          > image9.jpg
          > image10.jpg
          > image11.jpg
          > image12.jpg
          >
          > Using array.sort() here is how it gets sorted,
          >
          > image1.jpg
          > image10.jpg
          > image11.jpg
          > image12.jpg
          > image2.jpg
          > image3.jpg
          > image4.jpg
          > image5.jpg
          > image6.jpg
          > image7.jpg
          > image8.jpg
          > image9.jpg
          >
          > Any ideas how to correct this? I've tried a few methods[/color]
          unsuccssfully.[color=blue]
          >
          > David[/color]

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">
          <html>
          <head>
          <script type="text/javascript">

          Array.prototype .sortPix = function()
          {
          return this.sort(
          function(a, b)
          {
          return (a.match(/\d+/) - b.match(/\d+/));
          }
          );
          }

          var pix = [
          'image7.jpg' ,
          'image9.jpg' ,
          'image4.jpg' ,
          'image11.jpg' ,
          'image10.jpg' ,
          'image01.jpg' ,
          'image6.jpg' ,
          'image12.jpg' ,
          'image03.jpg' ,
          'image8.jpg' ,
          'image2.jpg' ,
          'image5.jpg'
          ];

          var sorted = pix.sortPix();
          alert(sorted.jo in('\n'));

          </script>
          </head>
          <body>
          </body>
          </html>

          Comment

          • Mick White

            #6
            Re: sort()

            Evertjan. wrote:
            [color=blue]
            > David wrote on 04 mei 2005 in comp.lang.javas cript:
            >
            >[color=green]
            >>sorry for the last post, itchy fingers.
            >>
            >>I'm having a bit of difficulty sorting images named in sequential
            >>numerical order. Here are the image names and how I need them sorted.
            >>
            >>image1.jpg
            >>image2.jpg
            >>image3.jpg
            >>image4.jpg
            >>image5.jpg
            >>image6.jpg
            >>image7.jpg
            >>image8.jpg
            >>image9.jpg
            >>image10.jpg
            >>image11.jpg
            >>image12.jpg
            >>
            >>Using array.sort() here is how it gets sorted,
            >>
            >>image1.jpg
            >>image10.jpg
            >>image11.jpg
            >>image12.jpg
            >>image2.jpg
            >>image3.jpg
            >>image4.jpg
            >>image5.jpg
            >>image6.jpg
            >>image7.jpg
            >>image8.jpg
            >>image9.jpg
            >>
            >>Any ideas how to correct this? I've tried a few methods unsuccssfully.[/color]
            >
            >
            > Why, it is a corect result of an alphanumeric sort?
            >
            > [see my earlier posting]
            >
            > However, if you want a numerical sort:
            >
            > =============== ============
            >
            > s = "image4.jpg,ima ge9.jpg,image11 1.jpg,image2.jp g,image1.jpg"
            >
            > s = s.split(',').so rt(compare).joi n(',')
            >
            > document.write( s)
            >
            > function compare(a, b) {
            > a = +a.replace(/image/g,'').replace(/\.jpg/g,'')
            > b = +b.replace(/image/g,'').replace(/\.jpg/g,'')
            > if (a<b) return -1
            > if (a>b) return 1
            > return 0
            > }[/color]
            or:

            function compare(a,b){
            return a.replace(/\D/g,'') - b.replace(/\D/g,'');
            }
            s = "image4.jpg,ima ge9.jpg,image11 1.jpg,image2.jp g,image1.jpg"
            alert(s.split(" ,").sort(compar e).join("\n"))

            Mick


            [color=blue]
            >
            > =============== =============
            >
            > this returns:
            >
            > image1.jpg,imag e2.jpg,image4.j pg,image9.jpg,i mage111.jpg
            >[/color]

            Comment

            • David

              #7
              Re: sort()

              [color=blue]
              > For the names you have, this is the correct alphanumeric sort - the
              > sort routine doesn't know to treat the digits at the end of the name
              > as numbers. Rename your images so they always have the same number of
              > digits in the name, and that will fix it (i.e. image01.jpg,
              > image02.jpg...i mage09.jpg,imag e10.jpg).
              >
              > Of course, this will break if you use more than 99 images and you'd
              > have to add another digit!
              >
              > Paul[/color]

              If it were that simple as to rename the images I would, but this will be an
              application where users can upload thier own images with thier own naming
              conventions so I need to make corrections for this neforehand.

              David


              Comment

              • David

                #8
                Re: sort()


                You guys are great and have given me things to try. Your examples do work
                but not exactly the way it needs to be. I have no control over the image
                names. This will be determined by "users", but they need to be sorted in a
                specific manner because "users" are dumb and use all kinds of naming
                conventions so I'm trying to correct for this beforehand in the script.

                Your codes do work but they sort soley by numerical. So imagine the images
                are named like this..

                lightimage1.jpg
                darkimage1.jpg
                darkimage10.jpg
                blueimage2.jpg

                Sorting them alphanumericaly only will result in this..

                darkimage10.jpg ,darkimage1.jpg ,blueimage2.jpg ,lightimage1.jp g


                Sorting them numericaly only will result in this..

                blueimage2.jpg, darkimage1.jpg, lightimage1.jpg ,darkimage10.jp g


                There needs to be a way to sort them so the result is always like this..

                blueimage2.jpg, darkimage1.jpg, darkimage10.jpg ,lightimage1.jp g

                It sorts the image names alphanumericall y but .. those that have numerical
                endings will be sorted in the correct order. Hard to explain but I think
                you know what I mean. Is this possible?

                David












                [color=blue]
                > function compare(a,b){
                > return a.replace(/\D/g,'') - b.replace(/\D/g,'');
                > }
                > s = "image4.jpg,ima ge9.jpg,image11 1.jpg,image2.jp g,image1.jpg"
                > alert(s.split(" ,").sort(compar e).join("\n"))
                >
                > Mick
                >
                >
                >[color=green]
                > >
                > > =============== =============
                > >
                > > this returns:
                > >
                > > image1.jpg,imag e2.jpg,image4.j pg,image9.jpg,i mage111.jpg
                > >[/color][/color]


                Comment

                • David

                  #9
                  Re: sort()

                  Correction to last post. I had the first example wrong in the
                  alphanumericall y only sorted. The blueimage2.jpg would come first in the
                  array.

                  Your codes do work but they sort soley by numerical. So imagine the images
                  are named like this..

                  lightimage1.jpg
                  darkimage1.jpg
                  darkimage10.jpg
                  blueimage2.jpg

                  Sorting them alphanumericaly only will result in this..

                  blueimage2.jpg, darkimage10.jpg ,darkimage1.jpg ,lightimage1.jp g


                  Sorting them numericaly only will result in this..

                  blueimage2.jpg, darkimage1.jpg, lightimage1.jpg ,darkimage10.jp g


                  There needs to be a way to sort them so the result is always like this..

                  blueimage2.jpg, darkimage1.jpg, darkimage10.jpg ,lightimage1.jp g


                  Comment

                  • Lee

                    #10
                    Re: sort()

                    David said:[color=blue]
                    >
                    >Correction to last post. I had the first example wrong in the
                    >alphanumerical ly only sorted. The blueimage2.jpg would come first in the
                    >array.
                    >
                    >Your codes do work but they sort soley by numerical. So imagine the images
                    >are named like this..
                    >
                    >lightimage1.jp g
                    >darkimage1.j pg
                    >darkimage10.jp g
                    >blueimage2.j pg
                    >
                    >Sorting them alphanumericaly only will result in this..
                    >
                    >blueimage2.jpg ,darkimage10.jp g,darkimage1.jp g,lightimage1.j pg
                    >
                    >
                    >Sorting them numericaly only will result in this..
                    >
                    >blueimage2.jpg ,darkimage1.jpg ,lightimage1.jp g,darkimage10.j pg
                    >
                    >
                    >There needs to be a way to sort them so the result is always like this..
                    >
                    >blueimage2.jpg ,darkimage1.jpg ,darkimage10.jp g,lightimage1.j pg[/color]

                    And what about "darkimage10a.j pg" and "darkimage10b.j pg"?
                    And what about the user who likes Roman Numerals?

                    Comment

                    • David

                      #11
                      Re: sort()

                      > And what about "darkimage10a.j pg" and "darkimage10b.j pg"?[color=blue]
                      > And what about the user who likes Roman Numerals?[/color]


                      Good point, but the sort for an image ending in an alphanumeric character
                      would then be sorted alphanumericall y so it isn't an issue, and it will sort
                      them properly "automatically" .


                      "darkimage1a.jp g"
                      "darkimage10a.j pg"
                      "darkimage10b.j pg"

                      but, even with these the problem still exists of the 1, 10 or 2, 20 etc..
                      syndrome.

                      "darkimage1a.jp g"
                      "darkimage10a.j pg"
                      "darkimage10b.j pg"
                      "darkimage1b.jp g"

                      when it should be

                      "darkimage1a.jp g"
                      "darkimage10a.j pg"
                      "darkimage1b.jp g"
                      "darkimage10b.j pg"

                      It's just the numbers that appear last in the image name with reference to
                      1, 10, 2, 20 etc..

                      David









                      Comment

                      • David

                        #12
                        Re: sort()

                        [color=blue]
                        > Good point, but the sort for an image ending in an alphanumeric character
                        > would then be sorted alphanumericall y so it isn't an issue, and it will[/color]
                        sort[color=blue]
                        > them properly "automatically" .[/color]

                        I should have said it will sort them properly "except" for the "last"
                        existing numbers in the image name, "if" these numbers are 1, 10 etc..
                        That's where the problem lies, in these "last" numbers in the image name.

                        David


                        Comment

                        • Lee

                          #13
                          Re: sort()

                          David said:[color=blue]
                          >[color=green]
                          >> And what about "darkimage10a.j pg" and "darkimage10b.j pg"?
                          >> And what about the user who likes Roman Numerals?[/color]
                          >
                          >
                          >Good point, but the sort for an image ending in an alphanumeric character
                          >would then be sorted alphanumericall y so it isn't an issue, and it will sort
                          >them properly "automatically" .[/color]
                          [color=blue]
                          >It's just the numbers that appear last in the image name with reference to
                          >1, 10, 2, 20 etc..[/color]

                          You need to put more thought into this.

                          alpha1
                          alpha2
                          alpha2a
                          alpha3
                          alpha10
                          alpha10a
                          alphabet1
                          alphabet2

                          Comment

                          • RobB

                            #14
                            Re: sort()

                            David wrote:[color=blue][color=green]
                            > > Good point, but the sort for an image ending in an alphanumeric[/color][/color]
                            character[color=blue][color=green]
                            > > would then be sorted alphanumericall y so it isn't an issue, and it[/color][/color]
                            will[color=blue]
                            > sort[color=green]
                            > > them properly "automatically" .[/color]
                            >
                            > I should have said it will sort them properly "except" for the "last"
                            > existing numbers in the image name, "if" these numbers are 1, 10[/color]
                            etc..[color=blue]
                            > That's where the problem lies, in these "last" numbers in the image[/color]
                            name.[color=blue]
                            >
                            > David[/color]

                            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                            "http://www.w3.org/TR/html4/strict.dtd">
                            <html>
                            <head>
                            <script type="text/javascript">

                            function sortPix(arr)
                            {
                            var temp = arr.sort();
                            return temp.sort(
                            function(a, b)
                            {
                            var aa, bb;
                            if ((aa = a.replace(/\d/g, '')) == (bb = b.replace(/\d/g, '')))
                            return a.match(/\d+/) - b.match(/\d+/);
                            else return aa > bb;
                            }
                            );
                            }

                            var pix =
                            [
                            'hot_potato4.jp g' ,
                            'lightimage1.jp g' ,
                            'darkimage10b.j pg' ,
                            'foo_pix4.png' ,
                            'darkimage1.jpg ' ,
                            'darkimage10.pn g' ,
                            'darkimage1a.jp g' ,
                            'blueimage2.jpg ' ,
                            'image22.gif' ,
                            'darkimage1b.jp g' ,
                            'bigpic.jpeg' ,
                            'red_img.gif' ,
                            'hot_potato22.j pg' ,
                            'hot_potato2.jp g' ,
                            'foo_pix3.png' ,
                            'darkimage10a.j pg'
                            ];

                            var sorted = sortPix(pix);
                            alert(sorted.jo in('\n'));

                            </script>
                            </head>
                            <body>
                            </body>
                            </html>

                            For some reason this doesn't work in FF, just ie6win. No idea why.
                            Is this the desired sort? Can we do this with php? #:-)

                            Comment

                            • Lasse Reichstein Nielsen

                              #15
                              Re: sort()

                              "David" <right@dd.com > writes:
                              [color=blue]
                              > There needs to be a way to sort them so the result is always like this..
                              >
                              > blueimage2.jpg, darkimage1.jpg, darkimage10.jpg ,lightimage1.jp g[/color]
                              [color=blue]
                              > It sorts the image names alphanumericall y but .. those that have numerical
                              > endings will be sorted in the correct order. Hard to explain but I think
                              > you know what I mean. Is this possible?[/color]

                              Everything is possible, if you can specify the desired outcome clearly.

                              A very general method for comparing strings containing numbers is
                              numCmp here:
                              ---
                              function cmp(a,b) { // standard comparison.
                              return (b<a)-(a<b);
                              }

                              function numCmp(a,b) {
                              var re1 = /(\d+)|\D+/g;
                              var re2 = /(\d+)|\D+/g;
                              re1.lastIndex = 0;re2.lastIndex =0; // Opera 8 bug.
                              var res = 0;
                              do {
                              match1 = re1.exec(a);
                              match2 = re2.exec(b);
                              if (match1) {
                              if (match2) {
                              if (match1[1]) {
                              if (match2[1]) { // fully numeric.
                              res = Number(match1[1]) - Number(match2[1]) || cmp(match1[0],match2[0]);
                              } else {
                              res = -1;
                              }
                              } else {
                              if (match2[1]) {
                              res = 1;
                              } else {
                              res = cmp(match1[0],match2[0]);
                              }
                              }
                              } else {
                              res = 1;
                              }
                              } else {
                              if (match2) {
                              res = -1;
                              } else {
                              res = 0; break;
                              }
                              }
                              } while (res == 0);
                              return res;
                              }

                              ---

                              It splits the strings into blocks of digits and non-digits respectively,
                              and then compares the digit-blocks as numbers. More precisely, it sorts
                              as if each digit block was one character that comes before any normal
                              character. If the numbers are identical, their string representation is
                              compared normally to disambiguate, i.e., "ab01" comes before "ab1".

                              Example:
                              ---
                              var l = ["ab10", "ab!", "ab2", "ab02", "ab0"];
                              var ls = l.sort(numCmp);
                              alert(ls); // ab0, ab02, ab2, ab10, ab!
                              ---

                              It's not a very effective way to sort, since each comparison splits
                              both strings into blocks again. For efficiency, you could split the
                              strings into blocks first, so it's only done once per string, and then
                              sort using these arrays as keys.

                              Good luck.
                              /L
                              --
                              Lasse Reichstein Nielsen - lrn@hotpop.com
                              DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                              'Faith without judgement merely degrades the spirit divine.'

                              Comment

                              Working...