Next and Previous Page links based on 'Order' attribute
I'm trying to add Next and Previous links to my site Pages (not Posts) to navigate based on the 'Order' information. I copied some code from the Codex (https://codex.wordpress.org/Next_and_Previous_Links) that works fine (displays the pages in the right order - not cronological) except on the first and last pages where I get an error. The error shows Undefined array key where the Previous link on the first page should be and the same error (different array entry) where the Next link should be on the last page. Looks like an index error but I'm not a confident enough PHP programmer to figure it out. A lot of other solutions (plugins) link the pages cronologicaly and I want to be able to rearrange the page order (using Simple Page Ordering plugin by Jake Goldman).