arraylist.sort的问题 [英] problem with arraylist.sort

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

问题描述




我在使用arraylist.sort时遇到问题。我正在使用

FileInfo.GetFiles()来接收来自

目录的文件列表。这些文件有一个序号开始

然后是A和A。附在它的末尾或

没有。例如,文件名看起来像

this:" 1A.jpg" " 2A.jpg" " 3.JPG" " 19A.jpg"当我使用FileInfo.GetFiles()时,它不会按排序顺序返回

文件列表。然后我尝试将每个

文件名作为字符串添加到数组列表中,然后

调用MyArrayList.sort()。。排序doenst工作

正确。它列出了10A.jpg。首先通过18A.jpg

,然后说19.jpg。那么1A.jpg那么20A.jpg

到29A.jpg那么2A.jpg那么30A.jpg

到36A.jpg那么3A.jpg通过9A.jpg。


你可以看到它的一半排序。我不理解
,因为如果我查看windows中的目录

资源管理器,它们会被正确排序。我错过了吗?
什么东西?


谢谢


Amit

解决方案

>你可以看到它的一半排序。我不

理解,因为如果我查看windows
explorer中的目录,它们就会被正确排序。我错过了什么?







它们按字母顺序排序正确,但问题是

它们也包含数字,你如何在字母表中输入数字?

当排序算法通过字符串运行时,它不会像你那样对它进行排序

可以预期,它会按照每个字符串的单个字符代码对它们进行排序,并且它也会独立地评估它们。


例如,你输入字符串


" mypicture14.jpg"


排序算法将其视为......


109,121,112,105,99,116,117,114,101,49,52,46,106,11 2,103

所以当比较2个字符串19.jpg时和1A.jpg他们出来了

as ..


19.jpg = 49,57,46,106,112,103

1A.jpg = 49,65 ,46,106,112,103


因此,19.jpg先来。这对你有用吗?即使我已经用b
了解它:-\


尼克。


-

/ \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \\ \\ / \\ \\ / / \\ \\ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \

无论如何。无论结果如何,你都会被改变。


Fergus - 2003年9月5日

/ \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \\ \\ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \


你好,


" Amit" <微米******* @ hotmail.com> schrieb im Newsbeitrag

news:04 **************************** @ phx.gbl ... < blockquote class =post_quotes>你好

我在使用arraylist.sort时遇到了问题。我正在使用
FileInfo.GetFiles()来接收来自
目录的文件列表。这些文件有一个序列号开始
然后是A和A。附在它的末尾或
没有。例如,文件名看起来像
这个:1A.jpg " 2A.jpg" " 3.JPG" " 19A.jpg"当我使用FileInfo.GetFiles()时,它不会按排序顺序返回
文件列表。然后我尝试将每个
文件名作为字符串添加到数组列表中,然后调用MyArrayList.sort()。排序也正常工作
正确。它列出了10A.jpg。首先通过18A.jpg
然后说19.jpg。那么1A.jpg那么20A.jpg
到29A.jpg那么2A.jpg那么30A.jpg
到36A.jpg那么3A.jpg通过9A.jpg。

你可以看到它的一半排序。我不明白,因为如果我查看windows
explorer中的目录,它们就会被正确排序。我错过了什么?




您希望如何对名称进行排序? IMO它们按照我的要求排序

期望它们被分类。


;-)


-

Herfried K. Wagner

MVP·VB Classic,VB.NET
http://www.mvps.org/dotnet


Hi Anit,

排序往往是字面上的工作。你和我知道1A和1A。来自

" 19A"但对于您的平均分拣程序19A,比1A更不。因为

''9''在排序序列中位于''A'之前。这已经是多年来排序的方式了,并且几十年来让很多人感到沮丧,

包括我。


你必须实现你自己的排序比较,它可以取

数值,然后先按照后面的顺序排序。


你''我需要这个班级做实际的比较。你可以把它放在

代码中,就在你对字符串进行排序的例行程序之前或之后,如果你好像b
那样。编译器不会反对。这将使FooComparer成为一个嵌套类。

[或者你可以把它放在顶部或其他文件中。在哪里放

这是一个完整的争论吧!]


类ArrayFooComparer:实现IComparer

公共函数比较(ByVal作为对象,ByVal B作为对象)_

作为整数实现IComparer.Compare

Dim I作为整数= CInt(Val(A) - Val(B))

如果我< 0 OrElse I> 0然后返回I

返回String.Compare(A.ToString,B.ToString)

结束功能

结束班级

然后你必须告诉数组使用你的比较例程,而不是(bumb)默认的



Array.Sort(ArrayFoo,New ArrayFooComparer)


我使用了''ArrayFoo'这个名字。您可以使用您喜欢的内容。

其他所有内容都应该是给定的。


如果您对此有任何疑问,请回来。 :-)


问候,

Fergus


Hi

I''m having a problem using arraylist.sort. I''m using
FileInfo.GetFiles() to receive a list of files from a
directory. The files have a sequential number to start
then theres either an "A" appended to the end of it or
there isn''t. For example, a file name will look like
this: "1A.jpg" "2A.jpg" "3.jpg" "19A.jpg" etc. When I
use FileInfo.GetFiles() it doesnt return the list of
files in a sorted order. So then I tried to add each
file name as a string into an array list, then
call "MyArrayList.sort()." The sort doenst work
correctly either. It lists "10A.jpg" through "18A.jpg"
first, then says "19.jpg" then "1A.jpg" then "20A.jpg"
through "29A.jpg" then "2A.jpg" then "30A.jpg"
through "36A.jpg" then "3A.jpg" through "9A.jpg".

As you can see its half way sorting them. I dont
understand because if I look at the directory in windows
explorer they are sorted correctly. Am I missing
something?

Thanks

Amit

解决方案

> As you can see its half way sorting them. I dont

understand because if I look at the directory in windows
explorer they are sorted correctly. Am I missing
something?



Hi,

They are being alphabetically sorted correctly, but the problem being
that they contain numbers too, how would you fit numbers into the alphabet?
When the sorting algorithm runs through a string it is not sorting it as you
would expect, it is sorting them by the individual character codes that make
up each string, and it is also elvaluating them independently.

For example, you put in the string

"mypicture14.jpg"

the sorting algorithm sees it as...

109,121,112,105,99,116,117,114,101,49,52,46,106,11 2,103

So when it compares the 2 strings "19.jpg" and "1A.jpg" they come out
as..

19.jpg = 49,57,46,106,112,103
1A.jpg = 49,65,46,106,112,103

Hence "19.jpg" coming first. Does this make sence to you? even though I
have explained it rather strangly :-\

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\


Hello,

"Amit" <um*******@hotmail.com> schrieb im Newsbeitrag
news:04****************************@phx.gbl...

Hi

I''m having a problem using arraylist.sort. I''m using
FileInfo.GetFiles() to receive a list of files from a
directory. The files have a sequential number to start
then theres either an "A" appended to the end of it or
there isn''t. For example, a file name will look like
this: "1A.jpg" "2A.jpg" "3.jpg" "19A.jpg" etc. When I
use FileInfo.GetFiles() it doesnt return the list of
files in a sorted order. So then I tried to add each
file name as a string into an array list, then
call "MyArrayList.sort()." The sort doenst work
correctly either. It lists "10A.jpg" through "18A.jpg"
first, then says "19.jpg" then "1A.jpg" then "20A.jpg"
through "29A.jpg" then "2A.jpg" then "30A.jpg"
through "36A.jpg" then "3A.jpg" through "9A.jpg".

As you can see its half way sorting them. I dont
understand because if I look at the directory in windows
explorer they are sorted correctly. Am I missing
something?



How would you expect the names to be sorted? IMO they are sorted as I would
expect them to be sorted.

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet


Hi Anit,

Sorting tends to work literally. You and I know that "1A" comes before
"19A" but to your average sorting routine "19A" is ''less'' than "1A" because
the ''9'' comes before ''A'' in its sorting sequence. This has been the way of
sorting for many years and has dismayed many people over the decades,
including me.

You will have to implement your own sort comparison which can take the
numeric value and sort by that first and then by whatever else follows.

You''ll need this class to do the actual comparing. You can put it in the
code just before or after the routine in which you sort your strings, if you
like. The compiler won''t object. This will make FooComparer a nested class.
[Or you could put it somewhere up at the top, or in another file. Where to put
it is a whole debate in iteself!]

Class ArrayFooComparer : Implements IComparer
Public Function Compare (ByVal A As Object, ByVal B As Object) _
As Integer Implements IComparer.Compare
Dim I As Integer = CInt (Val(A) - Val(B))
If I < 0 OrElse I > 0 Then Return I
Return String.Compare (A.ToString, B.ToString)
End Function
End Class
Then you have to tell the array to use your comparison routine instead of
the (dumb) default.

Array.Sort (ArrayFoo, New ArrayFooComparer)

I''ve used the name ''ArrayFoo''. You can use what you like instead.
Everything else, though, should be as given.

Come back if you have any questions about this. :-)

Regards,
Fergus


这篇关于arraylist.sort的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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