I have problem with sort()

username: Yolkandwhite
cooked: <p>hi I’m having difficulties with making datasets.<br>
I have 2 folders. One with image files and the other one with the mask files.<br>
I tried to make the data in order using sort()<br>
the image data were lined like <strong>1</strong>, 10, 11, 12, 13<br>
but mask data were like 10, 11, 12, 13, …, 19, <strong>1</strong></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/3/938ce29a0e446cbcd206752b6d3a39ec1aa653e6.png" data-download-href="https://discuss.pytorch.org/uploads/default/938ce29a0e446cbcd206752b6d3a39ec1aa653e6" title="image01"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/938ce29a0e446cbcd206752b6d3a39ec1aa653e6_2_690x328.png" alt="image01" data-base62-sha1="l3i23IHJleabiyeWZFVL6Ytut0i" width="690" height="328" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/938ce29a0e446cbcd206752b6d3a39ec1aa653e6_2_690x328.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/938ce29a0e446cbcd206752b6d3a39ec1aa653e6_2_1035x492.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/938ce29a0e446cbcd206752b6d3a39ec1aa653e6_2_1380x656.png 2x" data-dominant-color="E7E8E8"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image01</span><span class="informations">1604×763 1000 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/4/24da5c7e248bb1a3af0db22f50d23e33867b8002.png" data-download-href="https://discuss.pytorch.org/uploads/default/24da5c7e248bb1a3af0db22f50d23e33867b8002" title="image02"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24da5c7e248bb1a3af0db22f50d23e33867b8002_2_690x326.png" alt="image02" data-base62-sha1="5g11BpX2Yd4YYf6Ai46xFBLcYTg" width="690" height="326" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24da5c7e248bb1a3af0db22f50d23e33867b8002_2_690x326.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24da5c7e248bb1a3af0db22f50d23e33867b8002_2_1035x489.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24da5c7e248bb1a3af0db22f50d23e33867b8002_2_1380x652.png 2x" data-dominant-color="E7E7E7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image02</span><span class="informations">1468×694 926 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>how can I solve this problem??</p>

username: G.M
cooked: <p>I don’t see what is the problem, can you explain some more？</p>

username: Yolkandwhite
cooked: <p>Mask files and image files has the exact same name but the mask files have ‘_mask’ on the back.<br>
So If I use sort() for both image files and mask files, the order should be the same<br>
But it didn’t just I mentioned above</p>

username: ptrblck
cooked: <p>Based on your file names, the order is also wrong, as you didn’t prepend zeros to the actual number.<br>
E.g. after <code>1</code> comes the file with <code>10</code> in your images.<br>
You could use <code>sorted</code> with a lambda function as the <code>key</code> argument, get the desired file index and ignore the rest of the string.</p>

username: Yolkandwhite
cooked: <p>thank you I solved this problem using sorted with lambda function.<br>
but I don’t understand this part.<br>
what’s the meaning of</p>
<aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="73341">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>you didn’t prepend zeros to the actual number.</p>
</blockquote>
</aside>
<p>?</p>

username: ptrblck
cooked: <p>If you prepend leading zeros to the numbers, Python should be able to sort them properly.<br>
E.g.:</p>
<pre><code class="lang-python">my_list = sorted([str(i) for i in range(20)])
print(my_list) # wrong order

my_list = sorted(["{:04d}".format(i) for i in range(20)])
print(my_list) # right order
</code></pre>