1

I have multiple folders named

['8.5r1', '8.5r2', '8.5r3', '8.5r4', '8.5r41', '8.5r5', '81.5r3', '9.1r1'] 

When I am sorting them in python I am getting as

['8.5r1', '8.5r2', '8.5r3', '8.5r4', '8.5r5', '8.5r41', '9.1r1', '81.5r3']

But I need them as

['8.5r1', '8.5r2', '8.5r3', '8.5r4', '8.5r41', '8.5r5', '9.1r1', '81.5r3']

How to approach this?

Smruti Sahoo
  • 117
  • 7
  • You want **Human Sorting**. Try this. ```python import re def atoi(text): return int(text) if text.isdigit() else text def natural_keys(text): return [ atoi(c) for c in re.split(r'(\d+)', text) ] my_list=["8.5r2", "81.5r3", "9.1r1"] my_list.sort(key=natural_keys) print(my_list) ``` References: https://nedbatchelder.com/blog/200712/human_sorting.html https://blog.codinghorror.com/sorting-for-humans-natural-sort-order https://stackoverflow.com/questions/5967500/how-to-correctly-sort-a-string-with-a-number-inside – enesdemirag Jan 04 '21 at 12:16
  • To those who closed this question: the [linked duplicate](https://stackoverflow.com/a/18415320/1431750) - as good as it might be - requires using an external library. However, [the answer below](https://stackoverflow.com/a/65562520/1431750) (and [my comment](https://stackoverflow.com/questions/65562465/what-is-the-pythonic-way-of-sorting-folder-names-in-python-script#comment115915522_65562520)) don't require an external lib. So unless there's another duplicate (I'm sure there is) which is specific or doesn't require an external lib, this should be re-opened. – aneroid Jan 04 '21 at 12:39
  • 2
    @aneroid It doesn't require an external library. The accepted answer uses an external library, but many other answers are using built-in libraries or no libraries at all. – Ted Klein Bergman Jan 04 '21 at 12:52
  • @aneroid I can't use an external librabry. – Smruti Sahoo Jan 04 '21 at 13:08
  • @aneroid closing with a duplicate doesn't translate to *"Use the accepted answer in this question"*. It means *"Use any of the __19__ available answers in the other question"*... For example: https://stackoverflow.com/a/31432964/6045800. Also if you're so sure there is another duplicate - find it and link it here, then the question doesn't need to be reopened... – Tomerikoo Jan 04 '21 at 14:12
  • Maybe something like [Sorting a list of dot-separated numbers, like software versions](https://stackoverflow.com/questions/2574080/sorting-a-list-of-dot-separated-numbers-like-software-versions) – Tomerikoo Jan 04 '21 at 14:16
  • @Tomerikoo Okay, fair enough, there are enough other options in that linked dupe to answer the original question. Btw, they have [some additional requirements/conditions](https://stackoverflow.com/questions/65562465/what-is-the-pythonic-way-of-sorting-folder-names-in-python-script?noredirect=1#comment115916442_65562520) but unless they edit the question and answer the queries below, I'm happy to leave this closed. Withdrawing my reopen vote. – aneroid Jan 04 '21 at 14:20
  • _"Withdrawing my reopen vote."_ - apparently I can't withdraw it. – aneroid Jan 04 '21 at 14:21
  • @aneroid yep... see [How to revoke a reopen vote](https://meta.stackoverflow.com/questions/363384/how-to-revoke-a-reopen-vote) (even though the title make it sound like it - you can't...) – Tomerikoo Jan 04 '21 at 14:26
  • @Tomerikoo Noted. It'll either just timeout or get re-opened. Btw, the current version of the question wouldn't be solved by `natsort`, and would need to be adapted from one of the other answers but not in a very obvious way. So _now_ it could probably be re-opened. (Except that the starting list form is already sorted. lul.) :-/ – aneroid Jan 04 '21 at 14:32
  • 1
    @aneroid I think you'll agree with me (according to [your comment](https://stackoverflow.com/questions/65562465/what-is-the-pythonic-way-of-sorting-folder-names-in-python-script?noredirect=1#comment115917675_65562520)) that the current version of the question is not clear. So it being closed makes sense, the reason less important... – Tomerikoo Jan 04 '21 at 14:34
  • 1
    @SmrutiSahoo Take a look at the answer below and the comments - there is a way for you to use that and additionally sort further on the **2 dgits** (hint) after `r`. Also, read the [Sorting HowTo section on **Complex Sorts**](https://docs.python.org/3/howto/sorting.html#sort-stability-and-complex-sorts); for how you can go about it. – aneroid Jan 04 '21 at 14:39

1 Answers1

1

Assuming your data input is a list, try using sorted with a lambda in the key argument like below:

lst = ['8.5r2', '81.5r3', '9.1r1', '8.5r21']
print(sorted(lst, key=lambda x: list(map(float, x.split('r')))))

Output:

['8.5r2', '8.5r21', '9.1r1', '81.5r3']
U11-Forward
  • 41,703
  • 9
  • 50
  • 73
  • This fails if your starting list is `lst = ['8.5r21', '81.5r3', '9.1r1', '8.5r2']`. But if you change it to also compare the part after the 'r', it works: `print(sorted(lst, key=lambda x: list(map(float, x.split('r')))))` – aneroid Jan 04 '21 at 12:33
  • 2
    @aneroid Edited my answer; Should work now – U11-Forward Jan 04 '21 at 12:36
  • @U11-Forward My answer is failing for `lst = ['8.5r1', '8.5r2', '8.5r3', '8.5r4', '8.5r41', '8.5r5', '81.5r3', '9.1r1']` answer I am getting is : `['8.5r1', '8.5r2', '8.5r3', '8.5r4', '8.5r5', '8.5r41', '9.1r1', '81.5r3']` But I should get : `['8.5r1', '8.5r2', '8.5r3', '8.5r4', '8.5r41', '8.5r5', '9.1r1', '81.5r3']` – Smruti Sahoo Jan 04 '21 at 13:11
  • 2
    Why should release 41 `8.5r41` be sorted before release 5 `8.5r5`? Is 'r41' == 'r4.1'? And what do you do after release 10? Since r12 can be either release 1.2 or release 12. Are these release IDs you're creating or someone else has and now you need to add sorting to it? At the very least, release 4.1 should be like `8.5r4.1` or `8.5r4-1`. You need a delimiter. It can still be solved but then you need a "guarantee" that you'll never have 10 releases of the same version. Edit your question and add this example. Then it's different enough from the linked duplicate and might get re-opened. – aneroid Jan 04 '21 at 13:58