Numerically sort problem for a long list of file name

I got a long list of file name.
My input:
data_1.txt
data_2.txt
data_3.txt
data_10.txt
data_21.txt
data_12.txt
data_4.txt

My desired output:
data_1.txt
data_2.txt
data_3.txt
data_4.txt
data_10.txt
data_12.txt
data_21.txt

Does anybody got idea how to archive it?
I got try this code:

 
ls *.txt | sort -n

But it give the output like this:
data_1.txt
data_10.txt
data_12.txt
data_2.txt
data_21.txt
data_3.txt
data_4.txt

Which is not what I want :frowning:
Thanks a lot for suggestion.

If you can put all filenames in a file, try following command to get that file contents sorted.
sort -k 1,1n f1

sad to said that it can't work :frowning:
It give something like this:
data_10.txt
data_12.txt
data_1.txt
data_21.txt
data_2.txt
data_3.txt
data_4.txt

Which is not what I desired :frowning:
Still thanks for your help.

Try:

sort -t_ -k2n

Hi, it come out something like this as well:
data_1.txt
data_10.txt
data_12.txt
data_2.txt
data_21.txt
data_3.txt
data_4.txt

Do you have better suggestion?
Thanks again.

Are you sure? this command should work.

$ sort -t_ -k2n urfile
data_1.txt
data_2.txt
data_3.txt
data_4.txt
data_10.txt
data_12.txt
data_21.txt

Hi, you are right. It is worked.
But I feel quite weird that why it can't work if my content file name is like this:
amino_acids_10.txt
amino_acids_12.txt
amino_acids_21.txt
amino_acids_1.txt
amino_acids_2.txt
amino_acids_3.txt
amino_acids_4.txt

Do you know what is the reason?
I feel quite weird and confusing why the code only work for certain file content?

If you understand -t_, then you can adjust the code, such as:

sort -t_ -k3n urfile
sort -t"_." -n +1

Thanks a lot, rdcwayx.
It worked perfectly now.
Thanks a lot for sharing and advice ^^
I very appreciate it.

In your case:

sort -t_ -k3n infile

Because of the extra _ in the name.

Thanks a lot, Scrutinizer.
You are right. Thanks a lot for sharing info and your advice.
I finally solve my problem ^^