user19338: I am reading "Mastering Bitcoin written by Andreas" and I found this explanation of mining.

Transactions are added to the new block, prioritized by the
  highest-fee transactions first and a few other criteria.
Source: [Mastering Bitcoins: Mining Transactions in Blocks]

I want to know what the a few other criteria are. And I want to see the source code where the system prioritizes by highest-fee transactions first and a few other criteria.
I guess that the code is in miner.cpp. But I don't understand the exact line.
https://github.com/bitcoin/bitcoin/blob/master/src/miner.cpp

user18196: The code you're looking for is in CreateNewBlock. 
The usual way transactions are added to a block is by sorting by priority. The priority of an input is the value being spent in the input multiplied by the number of confirmations it has. The priority of a transaction is the sum of all the priorities of the inputs. See this line:
dPriority += (double)nValueIn * nConf;

Transactions are added to a vector of transaction priority:
vector<TxPriority> vecPriority;

Then they are sorted according to their priority:
TxPriorityCompare comparer(fSortedByFee);
std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);

And one by one, transactions are verified to be valid and then added to the block, done by order of priority, higher priority first.:
while (!vecPriority.empty())
...
// Added
pblock->vtx.push_back(tx);


19338: <p>I am reading "Mastering Bitcoin written by Andreas" and I found this explanation of mining.</p>

<blockquote>
  <p>Transactions are added to the new block, prioritized by the
  highest-fee transactions first and a few other criteria.</p>
  
  <p>Source: [<a href="http://chimera.labs.oreilly.com/books/1234000001802/ch02.html#_mining_transactions_in_blocks" rel="nofollow">Mastering Bitcoins: Mining Transactions in Blocks</a>]</p>
</blockquote>

<p>I want to know what the a few other criteria are. And I want to see the source code where the system prioritizes by highest-fee transactions first and a few other criteria.</p>

<p>I guess that the code is in <a href="https://github.com/bitcoin/bitcoin/blob/master/src/miner.cpp" rel="nofollow"><code>miner.cpp</code></a>. But I don't understand the exact line.</p>

<p><a href="https://github.com/bitcoin/bitcoin/blob/master/src/miner.cpp" rel="nofollow">https://github.com/bitcoin/bitcoin/blob/master/src/miner.cpp</a></p>
 18196: <p>The code you're looking for is in <a href="https://github.com/bitcoin/bitcoin/blob/v0.10.0rc3/src/miner.cpp#L91"><code>CreateNewBlock</code></a>. </p>

<p>The usual way transactions are added to a block is by sorting by priority. The priority of an input is the value being spent in the input multiplied by the number of confirmations it has. The priority of a transaction is the sum of all the priorities of the inputs. See <a href="https://github.com/bitcoin/bitcoin/blob/v0.10.0rc3/src/miner.cpp#L197">this line</a>:</p>

<pre><code>dPriority += (double)nValueIn * nConf;
</code></pre>

<p>Transactions are added to a <a href="https://github.com/bitcoin/bitcoin/blob/v0.10.0rc3/src/miner.cpp#L146">vector of transaction priority</a>:</p>

<pre><code>vector&lt;TxPriority&gt; vecPriority;
</code></pre>

<p>Then they are <a href="https://github.com/bitcoin/bitcoin/blob/v0.10.0rc3/src/miner.cpp#L225-L226">sorted according to their priority</a>:</p>

<pre><code>TxPriorityCompare comparer(fSortedByFee);
std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
</code></pre>

<p>And one by one, <a href="https://github.com/bitcoin/bitcoin/blob/v0.10.0rc3/src/miner.cpp#L228-L286">transactions are verified to be valid and then added to the block</a>, done by order of priority, higher priority first.:</p>

<pre><code>while (!vecPriority.empty())
...
// Added
pblock-&gt;vtx.push_back(tx);
</code></pre>
