分类() [英] sort()

查看:108
本文介绍了分类()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起最后一个帖子,手指发痒。


我在排序按顺序数字命名的图像时遇到一些困难

命令。以下是图片名称以及我如何对它们进行排序。


image1.jpg

image2.jpg

image3.jpg

image4.jpg

image5.jpg

image6.jpg

image7.jpg

image8.jpg

image9.jpg

image10.jpg

image11.jpg

image12.jpg


在这里使用array.sort()是如何排序的,


image1.jpg

image10.jpg

image11.jpg

image12.jpg

image2.jpg

image3.jpg

image4.jpg

image5.jpg

image6.jpg

image7.jpg

image8.jpg

image9.jpg


任何想法如何解决这个问题?我已经尝试了几种方法。


David


sorry for the last post, itchy fingers.

I''m having a bit of difficulty sorting images named in sequential numerical
order. Here are the image names and how I need them sorted.

image1.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg
image10.jpg
image11.jpg
image12.jpg

Using array.sort() here is how it gets sorted,

image1.jpg
image10.jpg
image11.jpg
image12.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg

Any ideas how to correct this? I''ve tried a few methods unsuccssfully.

David



推荐答案

David于2005年4月25日在comp.lang.javascript中写道
David wrote on 04 mei 2005 in comp.lang.javascript:
抱歉最后一篇文章,手指发痒。

我在排序按顺序
数字顺序命名的图像时遇到了一些困难。以下是图片名称以及我如何对它们进行排序。

image1.jpg
image2.jpg
image3.jpg
image4.jpg
image5。 jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg
image10.jpg
image11.jpg
image12.jpg

在这里使用array.sort()是如何排序的,

image1.jpg
image10.jpg
image11.jpg
image12.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8 .jpg
image9.jpg

任何想法如何纠正这个?我已经尝试了一些非常好的方法。
sorry for the last post, itchy fingers.

I''m having a bit of difficulty sorting images named in sequential
numerical order. Here are the image names and how I need them sorted.

image1.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg
image10.jpg
image11.jpg
image12.jpg

Using array.sort() here is how it gets sorted,

image1.jpg
image10.jpg
image11.jpg
image12.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg

Any ideas how to correct this? I''ve tried a few methods unsuccssfully.




为什么,它是字母数字排序的核心结果?


[见我早先的帖子]


但是,如果你想要一个数字排序:


=========== ================


s =" image4.jpg,image9.jpg,image111.jpg,image2.jpg,ima ge1。 jpg"


s = s.split('','')。sort(compare).join('','')


document.write(s)

函数比较(a,b){

a = + a.replace(/ image / g,'''' ).replace(/\ .jpg/g,'''')

b = + b.replace(/ image / g,'''')。replace(/\ .jpg/ g,'''')

if(a< b)return -1

if(a> b)返回1

返回0

}


============================ br />

返回:


image1.jpg,image2.jpg,image4.jpg,image9.jpg,image1 11.jpg


-

Evertjan。

荷兰。

(用我的电子邮件地址替换所有带点的十字架)



Why, it is a corect result of an alphanumeric sort?

[see my earlier posting]

However, if you want a numerical sort:

===========================

s = "image4.jpg,image9.jpg,image111.jpg,image2.jpg,ima ge1.jpg"

s = s.split('','').sort(compare).join('','')

document.write(s)

function compare(a, b) {
a = +a.replace(/image/g,'''').replace(/\.jpg/g,'''')
b = +b.replace(/image/g,'''').replace(/\.jpg/g,'''')
if (a<b) return -1
if (a>b) return 1
return 0
}

============================

this returns:

image1.jpg,image2.jpg,image4.jpg,image9.jpg,image1 11.jpg

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)


2005年5月4日星期三15:52 :格林尼治标准时间32,大卫 < RI *** @ dd.com>写道:
On Wed, 04 May 2005 15:52:32 GMT, "David" <ri***@dd.com> wrote:
对不起最后一篇文章,手指发痒。

我在顺序数字中排序图像时遇到了一些困难
订购。以下是图片名称以及我如何对它们进行排序。

image1.jpg
image2.jpg
image3.jpg
image4.jpg
image5。 jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg
image10.jpg
image11.jpg
image12.jpg

在这里使用array.sort()是如何排序的,

image1.jpg
image10.jpg
image11.jpg
image12.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8 .jpg
image9.jpg

任何想法如何纠正这个?我已经尝试了一些方法。

大卫


sorry for the last post, itchy fingers.

I''m having a bit of difficulty sorting images named in sequential numerical
order. Here are the image names and how I need them sorted.

image1.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg
image10.jpg
image11.jpg
image12.jpg

Using array.sort() here is how it gets sorted,

image1.jpg
image10.jpg
image11.jpg
image12.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg

Any ideas how to correct this? I''ve tried a few methods unsuccssfully.

David





你有,这是正确的字母数字排序 -

排序程序不知道将名字末尾的数字作为数字来对待

。重命名您的图像,以便它们在名称中始终具有相同数量的

数字,这将解决它(即image01.jpg,

image02.jpg ... image09 .jpg,image10.jpg)。


当然,如果您使用超过99张图像,这将会中断

必须添加另一张数字!


Paul



For the names you have, this is the correct alphanumeric sort - the
sort routine doesn''t know to treat the digits at the end of the name
as numbers. Rename your images so they always have the same number of
digits in the name, and that will fix it (i.e. image01.jpg,
image02.jpg...image09.jpg,image10.jpg).

Of course, this will break if you use more than 99 images and you''d
have to add another digit!

Paul


Paul Cooper在2005年4月的comp.lang.javascript中写道
Paul Cooper wrote on 04 mei 2005 in comp.lang.javascript:
对于您拥有的名称,这是正确的字母数字排序 -
排序例程不知道将名称末尾的数字视为数字
For the names you have, this is the correct alphanumeric sort - the
sort routine doesn''t know to treat the digits at the end of the name
as numbers.




是的,保罗,但是如果你教的话,它可以学习。

-

Evertjan 。

荷兰。

(用我的电子邮件地址替换所有带点的十字架)



True, Paul, but it can learn, if you teach it.
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)


这篇关于分类()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆