• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Sort a List with Integers & Letters
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Sort a List with Integers & Letters


  • Subject: Sort a List with Integers & Letters
  • From: Steven Valenti <email@hidden>
  • Date: Mon, 2 Apr 2007 16:23:22 -0400

Let's try this again...

I would like to sort a list of items that are currently strings. Most contain integers but some will also have letters.

set TheList to {"1610G", "121ZB", "114", "13", "21124", "121FB", "250G", "240.25", "1610", "15", "240 25", "240"}

My current routine will get the numbers in order but nothing is done for the letters or if a string of letters is added it will error. Does anyone already have a functional routine that sorts items much like the Finder would or how do I get around that letters are involved?

on ListSorter(TheList)
	set theSize to length of TheList
	repeat with i from 1 to theSize
		repeat with X from 2 to (theSize - i + 1)
			set Value_1 to (item (X - 1) of TheList)
			repeat
				try
					set Value_1 to (Value_1 as integer)
					exit repeat
				on error
					set Value_1 to (items 1 thru -2 of Value_1) as string
				end try
			end repeat

			set Value_2 to (item X of TheList)
			repeat
				try
					set Value_2 to (Value_2 as integer)
					exit repeat
				on error
					set Value_2 to (items 1 thru -2 of Value_2) as string
				end try
			end repeat

			if Value_1 > Value_2 then
				set temp to (item (X - 1) of TheList)
				set (item (X - 1) of TheList) to (item X of TheList)
				set (item X of TheList) to temp
			end if
		end repeat
	end repeat
	return TheList
end ListSorter

ListSorter(TheList)


_______________________________________________ Do not post admin requests to the list. They will be ignored. AppleScript-Users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
  • Follow-Ups:
    • Re: Sort a List with Integers & Letters
      • From: kai <email@hidden>
  • Prev by Date: Re: TUAW Script Fails
  • Next by Date: Re: libTIFF error
  • Previous by thread: Sort a List with Integers & Letters
  • Next by thread: Re: Sort a List with Integers & Letters
  • Index(es):
    • Date
    • Thread