1 Reply Latest reply on Jan 9, 2012 10:08 AM by lingoMaster_

    How to sort a list with numbers?

    mattias72

      Hi!

      How do i sort a list like this:

       

      ["Amningsnapp 6.c", "Anti-Colic Nappflaska 6.b", "Barn/Baby Underjams Girls 5.b", "Hår Gummisnodd 1.c", "Leksaker 7.a", "Pincett 2.a", "Protes Rengöring 3.a", "Sprayflaska Plast 4.b"]

       

      ...so that I get a list in number order like this instead:

       

      ["Hår Gummisnodd 1.c","Pincett 2.a","Protes Rengöring 3.a","Sprayflaska Plast 4.b","Barn/Baby Underjams Girls 5.b","Anti-Colic Nappflaska 6.b","Amningsnapp 6.c","Leksaker 7.a"]

       

       

      ------------------------------------------------------------------------

      On mouseUp

       

      currentItem = the mouseLine

       

        if the doubleClick = TRUE then

          handlingslista.add (field(12).line[currentItem])

          sort handlingslista

          

          set x = count (handlingslista)

          repeat with i = 1 to x

            put handlingslista[i] into member(17).line[i]

          end repeat

        end if

       

      end

      -------------------------------------------------------------------

      --This is what the list handlingslista contains:

       

      ["Amningsnapp 6.b", "Anti-Colic Nappflaska 6.b", "Barn/Baby Underjams Girls 5.b", "Hår Gummisnodd 1.c", "Leksaker 7.a", "Pincett 2.a", "Protes Rengöring 3.a", "Sprayflaska Plast 4.b"]

        • 1. Re: How to sort a list with numbers?
          lingoMaster_

          Assuming the number always occurs as the last word, you could temporarily store the items in the form ["6.c", "Hår Gummisnodd 1.c"], therefore resulting them being sorted lexicographically based on the last word, like this:

           

          handlingslista = ["Amningsnapp 6.c", "Anti-Colic Nappflaska 6.b", "Barn/Baby Underjams Girls 5.b", "Hår Gummisnodd 1.c", "Leksaker 7.a", "Pincett 2.a", "Protes Rengöring 3.a", "Sprayflaska Plast 4.b"]
          
          repeat with n = handlingslista.count down to 1
            handlingslista[n] = [the last word of handlingslista[n], handlingslista[n]]
          end repeat
          
          handlingslista.sort()
          
          repeat with n = handlingslista.count down to 1
            handlingslista[n] = handlingslista[n][2]
          end repeat
          
          put handlingslista
           -- ["Hår Gummisnodd 1.c", "Pincett 2.a", "Protes Rengöring 3.a", "Sprayflaska Plast 4.b", "Barn/Baby Underjams Girls 5.b", "Anti-Colic Nappflaska 6.b", "Amningsnapp 6.c", "Leksaker 7.a"]
          

           

          Alternatively, you could use Javascript, in which Arrays can be sorted based on an arbitrary ordering: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort.