AppleScript script unable to respond to a script statement correctly

Ilya Shebalin

I wrote a script which re-arranges items in a continuous order. The items are records each  containing two properties: text which is a paragraph of the original text and a number which denotes the paragraph's position in the original text. The items are to be ordered by placing them according to the numbered parameter values so that I could easily reverse the order by simply applying reverse property of list class to get the items sorted in a continuous ascending order. The script is as follows:

set RecList to {{ParagraphText:"On 18 Jun 2018, at 3:28 pm, Ilya Shebalin <iljashebalin2@...> wrote:", ParagraphPosition:1}, {ParagraphText:"That's how the as \"class utf8\" compiles in ScriptDebugger:", ParagraphPosition:3}, {ParagraphText:"", ParagraphPosition:4}, {ParagraphText:"Type this into a Script Debugger window:", ParagraphPosition:5}, {ParagraphText:"", ParagraphPosition:6}, {ParagraphText:"current date", ParagraphPosition:7}, {ParagraphText:"Compile, then go to the View Menu and choose Show Raw (Chevron) Syntax, and compile again. What do you see now?", ParagraphPosition:9}, {ParagraphText:"", ParagraphPosition:2}, {ParagraphText:"", ParagraphPosition:8}}
set NewRecList to {}
repeat until RecList is {}
repeat with itemRef in RecList
if ParagraphPosition of contents of itemRef = (count RecList) then
"log (count RecList):"
log (count RecList)
"log ParagraphPosition of contents of itemRef = (count RecList):"
log (ParagraphPosition of contents of itemRef = (count RecList))
set end of NewRecList to contents of itemRef
set (contents of itemRef) to missing value --a problem here!
"log (contents of ItemRef):"
log (contents of itemRef)
set RecList to every record of RecList
"log RecList:"
log RecList
"log (count RecList):"
log (count RecList)
end if
end repeat
end repeat

return {List1:NewRecList, List2:RecList}

Notice the log statements. Despite the fact that all statements resolve to true and contains expected values NewRecList contains duplicates of some items leaving alone others whereas the logic does not seemingly imply that. It should assign missing value to an item and consider only records, but it fails for that particular items.  Tell me please what mistake I made.

Happy day, 

Join to automatically receive all group messages.