Question: how did the sorting hat get the sword of gryffindor?
Answer: How did Neville get the sword of Gryffindor? Specifically, when Voldemort set the Sorting Hat on Neville's head and then set it alight, the sword dropped down onto Neville's head, he pulled it out and then hacked off Nagini's head. The sword and the sorting hat are definitely linked by some form of magic.