AppleScript: Tags order is undefined

Trying to sort tags. DEVONthink reverses order of tags if one tries that. Did work in the past.

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

tell application id "DNtp"
		set theRecords to selected records
		if theRecords = {} then error "Please select a record"
		repeat with thisRecord in theRecords
			set theTags to tags of thisRecord
			--> {"2", "1", "5", "3", "4"}
			set theTags_sorted to my sortAList(theTags)
			--> {"1", "2", "3", "4", "5"}
			set tags of thisRecord to {} -- necessary
			delay 0.2
			set test_1 to tags of thisRecord
			--> {}
			set tags of thisRecord to theTags_sorted
			set test_2 to tags of thisRecord
			--> {"5", "4", "3", "2", "1"}
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

on sortAList(theList)
	set theArray to current application's NSArray's arrayWithArray:theList
	set theArray to theArray's sortedArrayUsingSelector:"localizedStandardCompare:"
	return theArray as list
end sortAList

The internal order of the tags is actually undefined but in the user interface they should be sorted anyway. What’s the purpose of this script?

Sorting is part of preparation for scripts that import data into other apps. I often import the same things more than once in different files, so instead of doing the sorting each time it makes more sense to do it once in DEVONthink.

I’m wondering why DEVONthink does “ad hoc” sorting. Wouldn’t it be more economic to sort only once after tags have been changed?

And I don’t understand why sorting via AppleScript doesn’t work anymone. I’ve sorted tags quite often and never encountered any problems (or maybe didn’t noticed them as I didn’t check all results). Does that mean I only had luck in the past?

The tags of a record are not a simple list (like in most apps) but depend on the database structure, therefore they have to be retrieved and sorted anyway. And sorting few items requires almost no time.

Do you remember which version definitely worked the way you wanted?

It definitely worked in May 2020 in this script.

This script doesn’t seem to set the tags, only the image’s keywords.

Sure, it’s just that I know I’ve tried sorting tags back then. If DEVONthink’s tags order is undefined I obviously did too little testing, I always ended up with the desired results. I’ll simply sort on each export/creation. Thanks!