var img = [
"IMG_COM_20220516_1150_41_1375.webp",
"IMG_COM_20220516_1150_41_13810.webp",
"IMG_COM_20220516_1150_41_1386.webp",
"IMG_COM_20220516_1150_41_1389.webp",
"IMG_COM_20220516_1150_41_13911.webp",
"IMG_COM_20220516_1150_41_13912.webp",
];
I want to sort this array by bigger number
when I try .sort() didn’t change
I want the result be like this
[
"IMG_COM_20220516_1150_41_1375.webp",
"IMG_COM_20220516_1150_41_1386.webp",
"IMG_COM_20220516_1150_41_1389.webp",
"IMG_COM_20220516_1150_41_13810.webp",
"IMG_COM_20220516_1150_41_13911.webp",
"IMG_COM_20220516_1150_41_13912.webp",
]
4
Answers
I found this online
fixing everything for me
To sort the array based on the numerical values within the strings, you can use a custom sort function with the
sort()
method. The custom sort function should extract the numerical part from the strings and compare them numerically. Here’s how you can do it:This will output the sorted array as you desired:
This example uses a regex to match value content and sort the numbers accordingly in descending order.
You can try this