Built-in AS methods for filtering a list

I have some code to work with an InDesign file that iterates through the selected objects to pull their parent page IDs and then I want to select the page that is always the right hand page of a spread if there is more than one object on more than one page selected. I thought there might be a built in AS method that finds the maximum value in the list of numbers or properties like page IDs.

Seems not, but I also seem to recall seeing something like

set startPage to item -1 in listOfPages -- where item -1 is the last in a list, item -2 is the second last etc etc
or
set startPage to last item in listOfPages

do I need to sort the list first, or are last and item -1 smart enough to do their own implicit sorting lowest to highest?!

here’s the code ChatGTP and I have so far that is not working:

	-- Get the current selection
	set mySelection to selection
	if (count mySelection) is not equal to 0 then
		-- Determine the page of the highest-numbered parent page of the selected objects
		set parentPages to {}
		repeat with selectedObject in mySelection
			set parentPage to parent page of selectedObject
			if parentPage is not missing value then
				set end of parentPages to index of parentPage
			end if
		end repeat
		if (count parentPages) > 0 then
			set myPage to last page of parentPages of myDocument
		end if
	else
		-- Use the last visible page of the current spread
		set activeSpread to active spread of myDocument
		set visiblePages to pages of activeSpread
		set myPage to last item of visiblePages -- Last visible page
	end if

If I understand you well, you are trying to get the rightmost page of selected items on the current spead…
If it’s correct, try this:

use framework "Foundation"
use scripting additions

tell application id "InDn"
	if not (exists active document) then return "no document"
	if not (exists selection) then return "no selection"
	try
		set thePages to document offset of parent page of selection
		if class of thePages ≠ list then set thePages to thePages as list
		set theSet to current application's NSSet's setWithArray:thePages
		set theDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(missing value) ascending:true
		set thePages to (theSet's sortedArrayUsingDescriptors:{theDescriptor}) as list
		id of page (last item of thePages) of active document
	on error
		return "bad selection"
	end try
end tell

But if you need to select the last page of the active spread only if there is page item selected:

tell application id "InDn"
	if not (exists active document) then return "no document"
	if not (exists selection) then return "no selection"
	try
		set lastPast to id of page -1 of active spread of active window
		set thePages to id of parent page of selection
		if class of thePages ≠ list then set thePages to thePages as list
		if lastPast is in thePages then
			select page id lastPast of active document
		else
			return "no selected item on the last page of the spread"
		end if
	on error
		return "bad selection"
	end try
end tell