sort()

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

    sort()

    I'm having a bit of difficulty sorting images named in sequential order.

    image1.jpg


  • Evertjan.

    #2
    Re: sort()

    David wrote on 04 mei 2005 in comp.lang.javas cript:[color=blue]
    > I'm having a bit of difficulty sorting images named in sequential order.
    >
    > image1.jpg[/color]

    This will do a alphanumeric sort:

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

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

    s = s.split(',').so rt().join(',')

    alert(s)

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

    resulting in:

    image1.jpg,imag e111.jpg,image2 .jpg,image4.jpg ,image9.jpg

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

    Comment

    • David

      #3
      Re: sort()

      Thanks Evertjan but I need the result like so...

      image1.jpg,imag e2.jpg,image4.j pg,image9.jpg ,image111.jpg

      David





      "Evertjan." <exjxw.hannivoo rt@interxnl.net > wrote in message
      news:Xns964CB61 B18CB5eejj99@19 4.109.133.29...[color=blue]
      > David wrote on 04 mei 2005 in comp.lang.javas cript:[color=green]
      > > I'm having a bit of difficulty sorting images named in sequential order.
      > >
      > > image1.jpg[/color]
      >
      > This will do a alphanumeric sort:
      >
      > =============== =======
      >
      > s = "image4.jpg,ima ge9.jpg,image11 1.jpg,image2.jp g,image1.jpg"
      >
      > s = s.split(',').so rt().join(',')
      >
      > alert(s)
      >
      > =============== ========
      >
      > resulting in:
      >
      > image1.jpg,imag e111.jpg,image2 .jpg,image4.jpg ,image9.jpg
      >
      > --
      > Evertjan.
      > The Netherlands.
      > (Replace all crosses with dots in my emailaddress)
      >[/color]


      Comment

      • Lee

        #4
        Re: sort()

        David said:[color=blue]
        >
        >I'm having a bit of difficulty sorting images named in sequential order.
        >
        >image1.jpg[/color]


        If you're asking for help, you'll have to give us more to work with.

        Comment

        • Dr John Stockton

          #5
          Re: sort()

          JRS: In article <eq6ee.28100$r8 1.1128@trnddc02 >, dated Wed, 4 May 2005
          16:06:34, seen in news:comp.lang. javascript, David <right@dd.com > posted
          :[color=blue]
          > I need the result like so...
          >
          >image1.jpg,ima ge2.jpg,image4. jpg,image9.jpg ,image111.jpg[/color]


          In such cases, it is far better to arrange that the numeric part is
          fixed-width, by using leading zeroes.


          If there is much data, I suggest first transform it to that form, then
          use a standard sort, then strip leading zeroes.


          Otherwise, use a user-defined comparison function :
          * Apply a RegExp to each of the comparands, splitting into letter and
          number parts.
          * Compare the letter parts, as strings, with > and <, returning +1 or -1
          accordingly
          * If not yet returned, return the difference of the numeric parts; use
          of subtraction forces conversion to numbers.

          Note that a comparison function is generally called more than N times in
          sorting N items, so the entries will be split more than 2N times.

          I don't have time to write and test code tonight.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          • David

            #6
            Re: sort()

            Thanks Dr. John,

            I think I have enough to go on thanks to the other responses. If I muck it
            up .. I'll be back :-)

            David





            "Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote in message
            news:sfCH7OJbPU eCFwmY@merlyn.d emon.co.uk...[color=blue]
            > JRS: In article <eq6ee.28100$r8 1.1128@trnddc02 >, dated Wed, 4 May 2005
            > 16:06:34, seen in news:comp.lang. javascript, David <right@dd.com > posted
            > :[color=green]
            > > I need the result like so...
            > >
            > >image1.jpg,ima ge2.jpg,image4. jpg,image9.jpg ,image111.jpg[/color]
            >
            >
            > In such cases, it is far better to arrange that the numeric part is
            > fixed-width, by using leading zeroes.
            >
            >
            > If there is much data, I suggest first transform it to that form, then
            > use a standard sort, then strip leading zeroes.
            >
            >
            > Otherwise, use a user-defined comparison function :
            > * Apply a RegExp to each of the comparands, splitting into letter and
            > number parts.
            > * Compare the letter parts, as strings, with > and <, returning +1 or -1
            > accordingly
            > * If not yet returned, return the difference of the numeric parts; use
            > of subtraction forces conversion to numbers.
            >
            > Note that a comparison function is generally called more than N times in
            > sorting N items, so the entries will be split more than 2N times.
            >
            > I don't have time to write and test code tonight.
            >
            > --
            > © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE[/color]
            4 ©[color=blue]
            > <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of[/color]
            news:comp.lang. javascript[color=blue]
            > <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates,[/color]
            sources.[color=blue]
            > <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items,[/color]
            links.


            Comment

            Working...