• 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
Re: Sorting a List of Lists
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sorting a List of Lists


  • Subject: Re: Sorting a List of Lists
  • From: Deivy Petrescu <email@hidden>
  • Date: Mon, 29 Apr 2013 11:51:17 -0400

On Apr 27, 2013, at 17:49 , Marconi <email@hidden> wrote:

> I have a list of lists with two items in each list.
>
> {"2013-04-01", "some text here"}
> {"2010-07-11", "some other text here"}
> {"2012-12-22", "some different here"}
>
> and so on. That is, my list of lists might be:
>
> {{"2013-04-01", "some text here"},{"2012-07-11", "some other text here"},{"2010-12-22", "some different here"}}
>
> Most of these lists of lists will be in the range of 100-200 item pairs in each list.
>
> I need to sort these on the first datum so that the above list would come out:
>
> {"2010-07-11", "some other text here"}
> {"2012-12-22", "some different here"}
> {"2013-04-01", "some text here"}
>
> I tried a generic bubble sort but get the error:
>
> Can't make {"2013-04-01", "some text here"} into type number, date or text.
>
> Treating "2013-04-01" as ASCII would be fine with me, though I suspect it's treating the dates as type number.
>
> What to do?

Others have pointed out what maybe better performance scripts, but here is your bubble sort:

<script>
set lista to {{"2013-04-01", "some text here"}, {"2010-07-11", "some other text here"}, {"2012-12-22", "some different here"}}

set j to 1
set cl to (count lista)

repeat
	set i1 to contents of item 1 of item j of lista
	set k to j + 1
	repeat with l in (items (j + 1) thru cl of lista)
		set l1 to contents of item 1 of l
		if i1 > (l1) then set {item j of lista, item k of lista, i1} to {item k of lista, item j of lista, l1} -- change > to < if you want descending sort
		set k to k + 1
	end repeat
	set j to j + 1
	if j + 1 > cl then exit repeat
end repeat
lista
</script>

I am sorting only according to the date. If two items have the same date the first one in the list comes first. If you want to sort alphabetically after the date remove "item 1 of" from the script

Deivy Petrescu
email@hidden




 _______________________________________________
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

References: 
 >Sorting a List of Lists (From: Marconi <email@hidden>)

  • Prev by Date: AppleScript to control Logic Pro? loading samples from a folder
  • Next by Date: Re: Epoch Seconds to YYYY-MM-DD
  • Previous by thread: Re: Sorting a List of Lists
  • Next by thread: AppleScript to control Logic Pro? loading samples from a folder
  • Index(es):
    • Date
    • Thread