Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort File-List in numerical order #955

Closed
wants to merge 1 commit into from
Closed

Conversation

pit-ray
Copy link

@pit-ray pit-ray commented Nov 17, 2021

Currently, if the following files exist, they are displayed in string order.

$ ls
image1.png image2.png image10.png image3.png image200.png

In labelme.

image1.png
image10.png
image2.png
image200.png
image3.png

However, I think it is natural to use a list of string numbers converted to numbers for sorting. I added a utility function for this to utils/_io.py because file names are i/o identifiers.

@wkentaro
Copy link
Owner

I'm not sure if this is a general solution? What happens if you have multiple digits splitted by _ or -?

1_1
1_100
1-10
1-11
1_1-1

@pit-ray
Copy link
Author

pit-ray commented Feb 26, 2022

This pull request seems to be closeing, but I show the result of the example below just in case.
The result is the same as natsorted in #990, and there is no dependency.

import re


def strnum_to_num(text):
    out = []
    for w in re.split(r"(\d+)", text):
        out.append(int(w) if w.isdigit() else w)
    return out

filenames = [
    "img_1.jpg",
    "img_100.jpg",
    "img_10.jpg",
    "img_11.jpg",
    "img_1-1.jpg",
    "img_12-1.jpg",
]

print("original:", filenames)
print(
    "numlist :",
    sorted(filenames, key=lambda x: strnum_to_num(x.lower()))
)

print()

filenames = [
    "image1.png",
    "image10.png",
    "image2.png",
    "image200.png",
    "image3.png",
]

print("original:", filenames)
print(
    "numlist :",
    sorted(filenames, key=lambda x: strnum_to_num(x.lower()))
)
original: ['img_1.jpg', 'img_100.jpg', 'img_10.jpg', 'img_11.jpg', 'img_1-1.jpg', 'img_12-1.jpg']
numlist : ['img_1-1.jpg', 'img_1.jpg', 'img_10.jpg', 'img_11.jpg', 'img_12-1.jpg', 'img_100.jpg']

original: ['image1.png', 'image10.png', 'image2.png', 'image200.png', 'image3.png']
numlist : ['image1.png', 'image2.png', 'image3.png', 'image10.png', 'image200.png']  

However, os_sorted in #990 is more natural.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants