Let’s say we have “この建物にいいレストランがあります”。Why does “建物に“come before “レストランが”？How do you know in what order the words are to be said and does it matter?
In Japanese, as a general rule, the more important a piece of information is, the closer it will go to the end of the sentence.
In 「この建物にいいレストランがあります」, the feeling is that the most important bit of information is the fact that there is a good restaurant. Whereas in 「いいレストランがこの建物にあります」, the feeling is that the most important bit of information is its location is in this building. However in this case, this distinction is very subtle and in most cases either would sound fine.
Japanese is very flexible with word order in clauses (and by extension, sentences); as long as your predicate (verb, copula, main adjective/nominal adjective) is at the end there are few limits to how you can rearrange the blocks of words delineated by particles. (I say it this way because you cannot mix up the order of things like adjectives and their modifying adverbs, or nouns and their modifying adjectives, nor can you break up a relative clause.)