problem with arraylist.sort

  • Thread starter Thread starter Amit
  • Start date Start date
A

Amit

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,112,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 said:
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.

;-)
 
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
 
This will make FooComparer a nested class.

I want to start nesting classes again, but the last time I done that I got
into an unrecoverable error with VB. It was something to do with the "new"
word, so I had to remove the "new" word from then entire class before
"un-nesting" it to get VB to work again. That was actually the first bug I
came across, nasty huh?

I suppose I should take the risk again simply for code compactness! :-)
Objects and their relevant collection object in the same class file... etc.

Nick.

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

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
Thanks for the reply

When I look in windows explorer I see the files listed
like this:

1A.jpg
2A.jpg
3A.jpg
5.jpg
5A.jpg
6A.jpg
..
..
..
19.jpg
19A.jpg
etc.

This is how I would like them to be sorted. Is there a
way to do this? If windows can sort them like this I'm
sure it can be done in VS.NET

Thanks

Amit
 
Hello,

Amit said:
When I look in windows explorer I see the files listed
like this:

1A.jpg
2A.jpg
3A.jpg
5.jpg
5A.jpg
6A.jpg
.
.
.
19.jpg
19A.jpg
etc.

This is how I would like them to be sorted. Is there a
way to do this? If windows can sort them like this I'm
sure it can be done in VS.NET

I think the file names are sorted without the ".jpg". You can use
'Path.GetFileNameWithoutExtension' to remove the extension. Just a thought.
 
Good evening Nick, :-)

Lol. You <do> seem to challenge VB. Either that or it doesn't like you
:-(.

Regards,
Fergus
 
Oh sweet!! It worked. Thanks so much. You have no idea
how long I've been struggling with this. Thanks Again

Amit
 
Hi Fergus,

Probably the latter :-(

Nick.

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

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