Sort tenors in finance notation
<p>I have an array of tenors</p>

<pre><code>Tenors = np.array(['10Y', '15Y', '1M', '1Y', '20Y', '2Y', '30Y', '3M', '5Y', '6M', '9M'])
</code></pre>

<p>where <code>M</code> stands for month and <code>Y</code> stands for years. The correctly sorted order (ascending) would then be</p>

<pre><code>['1M', '3M', '6M', '9M', '1Y', '2Y', '5Y', '10Y', '15Y', '20Y', '30Y']
</code></pre>

<p>How do I achieve that using python with scipy/numpy? As the <code>tenors</code> originate from a <code>pandas</code> dataframe a solution based on <code>pandas</code> would be fine as well.</p>

<pre><code>print sorted(Tenors, key=lambda Tenors: (Tenors[-1], int(Tenors[:-1])))
</code></pre>

<p>Sorts by the last character and then by the integer value up to the last character</p>

python sorting pandas numpy finance