How does miner order transactions if two are unrelated? e.g., A pays C, X pays Y. I assume changing the order in the block will change the hash, so I am curious how Bitcoin code combines them in which order.
Transactions can be ordered in a block in any way a miner wishes to so long as a transaction which spends an output created by another transaction in that block is placed after its parent. This order does matter for the block hash, and so once the order is decided, it is fixed for that block.
Many miners use Bitcoin Core's block template functionality to build their blocks. This will choose the transactions to include, and an order to include them in. The way Bitcoin Core chooses transactions is to first group related transactions together as packages, then order packages by the feerate paid by the package. Then packages are simply selected in order from highest to lowest until the block weight maximum is reached. (The actual selection is a bit more complicated than that, but this is the general idea of what it does.)
The result is that many blocks simply have their transactions in decreasing feerate order, with transactions that are dependent on each other being placed in order consecutively.
Do we compute hash based on the set of data after sorting?
The merkle root is computed with transactions in the order they are found in the block. So once an order is chosen, it is the only correct order for that block. Changing the order will change the merkle root and thus the block hash.
How does Bitcoin client code decide when to start the nonce + mining process? I understand it has some kind of batch process, so it doesn't start the mining until it can assemble some transactions into a block. But, where's the code to assemble those? Some say miner does whatever they want, but at the end of the day, nobody writes their own client code. There has to be some common code that implements this logic.
The actual miner and the node (that receives blocks and transactions and builds the block template) are separate software. The node, once it is synced, can build a block template at any time. Depending on how long that node has been online, the template it creates may not be ideal, but it is still usable. There is no requirement that blocks must have more than 1 transaction, so it is perfectly fine for a node to be creating block templates even when it is not aware of any unconfirmed transactions.
So a solo miner might just start their node, wait for it to sync, then start their mining software. There is not any smart determination of when to start mining. Miners generally want to start as soon as possible, as any block reward (starting immediately) is better than no block reward (waiting for transactions).