0

I am trying to work with files which needs to be used for downstream processing. The files in the subdirectory structure and the filenames are somewhat like :

  • ./resources/json
    -pdfextract_1_pdf.json
    -pdfextract_4_pdf.json
    -pdfextract_3_pdf.json
    -pdfextract_2_pdf.json

When I just try to sort just the files in the subdirectort ./resources/json. It works.

mylist = ['pdfextract_2_pdf.json','pdfextract_3_pdf.json','pdfextract_1_pdf.json']
mylist.sort()

This one seems to be not working. Eventhough everything looks similar (just a string)

['pf1109__r_td6831__425_a_b_c.pdf.page33.pdf_testing_xml.json', 'pf1109__r_td6831__425_a_b_c.pdf.page4.pdf_testing_xml.json', 'pf1109__r_td6831__425_a_b_c.pdf.page41.pdf_testing_xml.json']
Betafish
  • 1,212
  • 3
  • 20
  • 45
  • 1
    What do you mean by failed to work? Why are you mixing / and \ separators? –  Aug 07 '19 at 06:39
  • 3
    Works just fine in python 2.7 and python 3.5, 3.6 – Chris Aug 07 '19 at 06:40
  • 1
    Can't reproduce the problem. [I tried it on python 3.7](https://ideone.com/5XGVvp) and it works fine – Arun A S Aug 07 '19 at 06:41
  • @ArunAS, see the actual example (sorry, had to create a toy example). – Betafish Aug 07 '19 at 07:04
  • Possible duplicate of [How to sort a list of strings?](https://stackoverflow.com/questions/36139/how-to-sort-a-list-of-strings) – Dariusz Krynicki Aug 07 '19 at 07:08
  • From what I've understood, you say it's not working because page33 comes before page4 in your sort. I think this is what you're looking for [Sorting a list of strings numerically](https://stackoverflow.com/questions/52737587/sorting-a-list-of-strings-numerically) – Arun A S Aug 07 '19 at 08:06

2 Answers2

2

sorted() works perfectly well in python3

>>> mylist = ['./resources/json\\pdfextract_2_pdf.json','./resources/json\\pdfextract_3_pdf.json','./resources/json\\pdfextract_1_pdf.json']
>>> print(sorted(mylist))
['./resources/json\\pdfextract_1_pdf.json', './resources/json\\pdfextract_2_pdf.json', './resources/json\\pdfextract_3_pdf.json']
Gábor Erdős
  • 3,599
  • 4
  • 24
  • 56
  • I tried it with the below example. It doesnt seem to work. `['pf1109__r_td6831__425_a_b_c.pdf.page33.pdf_testing_xml.json', 'pf1109__r_td6831__425_a_b_c.pdf.page4.pdf_testing_xml.json', 'pf1109__r_td6831__425_a_b_c.pdf.page41.pdf_testing_xml.json']` – Betafish Aug 07 '19 at 07:08
  • @Betafish You may want this: https://stackoverflow.com/questions/3426108/how-to-sort-a-list-of-strings-numerically – Andrej Kesely Aug 07 '19 at 07:59
1

for descending order

sorted(mylist , reverse = True)

and for ascending order just use

sorted(mylist , reverse = false)

or

sorted(mylist)
Rajeev Shankhwar
  • 86
  • 1
  • 1
  • 8