Home All Groups Group Topic Archive Search About

Need to sort an Array

Author
9 Jul 2006 5:21 AM
Jack
Hello,

I have an array that won't sort correctly:

Dim MyArrayList As New ArrayList

Dim MyString As String = "5,3,44,22,20,2,40,1,32.50"

MyArrayList.AddRange(MyString .Split(","c))

MyArrayList .Sort()

For i As Integer = 0 To MyArrayList .Count - 2

Console.WriteLine(FormatNumber(MyArrayList .Item(i), 2, 0, 0, 0))

Next

I get:



1.00
2.00
20.00
22.00
3.00
32.50
40.00
44.00



I need:

1.00
2.00
3.00
20.00
22.00
32.50
40.00
44.00

Any help would be great!!!

Thanks in advance for you help,

Jack

Author
9 Jul 2006 6:14 AM
GhostInAK
Hello Jack,

Array.Sort is a function, not a sub.

-Boo

Show quoteHide quote
> Hello,
>
> I have an array that won't sort correctly:
>
> Dim MyArrayList As New ArrayList
>
> Dim MyString As String = "5,3,44,22,20,2,40,1,32.50"
>
> MyArrayList.AddRange(MyString .Split(","c))
>
> MyArrayList .Sort()
>
> For i As Integer = 0 To MyArrayList .Count - 2
>
> Console.WriteLine(FormatNumber(MyArrayList .Item(i), 2, 0, 0, 0))
>
> Next
>
> I get:
>
> 1.00
> 2.00
> 20.00
> 22.00
> 3.00
> 32.50
> 40.00
> 44.00
> I need:
>
> 1.00
> 2.00
> 3.00
> 20.00
> 22.00
> 32.50
> 40.00
> 44.00
> Any help would be great!!!
>
> Thanks in advance for you help,
>
> Jack
>
Author
9 Jul 2006 6:22 AM
Jack
Hello,

Thanks for your response, how do I get my ArrayList sorted correctly?

Thanks,

Jack
Show quoteHide quote
"GhostInAK" <ghosti***@gmail.com> wrote in message
news:c71747b427ba38c870f2fa27efb4@news.microsoft.com...
> Hello Jack,
>
> Array.Sort is a function, not a sub.
>
> -Boo
>
>> Hello,
>>
>> I have an array that won't sort correctly:
>>
>> Dim MyArrayList As New ArrayList
>>
>> Dim MyString As String = "5,3,44,22,20,2,40,1,32.50"
>>
>> MyArrayList.AddRange(MyString .Split(","c))
>>
>> MyArrayList .Sort()
>>
>> For i As Integer = 0 To MyArrayList .Count - 2
>>
>> Console.WriteLine(FormatNumber(MyArrayList .Item(i), 2, 0, 0, 0))
>>
>> Next
>>
>> I get:
>>
>> 1.00
>> 2.00
>> 20.00
>> 22.00
>> 3.00
>> 32.50
>> 40.00
>> 44.00
>> I need:
>>
>> 1.00
>> 2.00
>> 3.00
>> 20.00
>> 22.00
>> 32.50
>> 40.00
>> 44.00
>> Any help would be great!!!
>>
>> Thanks in advance for you help,
>>
>> Jack
>>
>
>
Author
9 Jul 2006 8:32 AM
Herfried K. Wagner [MVP]
"GhostInAK" <ghosti***@gmail.com> schrieb:
> Array.Sort is a function, not a sub.

'ArrayList.Sort' is a sub.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
13 Jul 2006 6:30 AM
GhostInAK
Hello Herfried K. Wagner [MVP],

DAMN.  This is what I get for spewing crap without looking it up first.

-Boo


Show quoteHide quote
> "GhostInAK" <ghosti***@gmail.com> schrieb:
>
>> Array.Sort is a function, not a sub.
>>
> 'ArrayList.Sort' is a sub.
>
Author
14 Jul 2006 2:11 AM
GhostInAK
Hello Herfried K. Wagner [MVP],

DAMN.  This is what I get for spewing crap without looking it up first.

-Boo

Show quoteHide quote
> "GhostInAK" <ghosti***@gmail.com> schrieb:
>
>> Array.Sort is a function, not a sub.
>>
> 'ArrayList.Sort' is a sub.
>
Author
14 Jul 2006 2:11 AM
GhostInAK
Hello Herfried K. Wagner [MVP],

DAMN.  This is what I get for spewing crap without looking it up first.

-Boo

Show quoteHide quote
> "GhostInAK" <ghosti***@gmail.com> schrieb:
>
>> Array.Sort is a function, not a sub.
>>
> 'ArrayList.Sort' is a sub.
>
Author
9 Jul 2006 8:35 AM
Oenone
Jack wrote:
Show quoteHide quote
> I have an array that won't sort correctly:
[...]
> Dim MyString As String = "5,3,44,22,20,2,40,1,32.50"
[...]
> I get:
>
> 1.00
> 2.00
> 20.00
> 22.00
> 3.00
> 32.50
> 40.00
> 44.00

Looks like it's sorted perfectly to me.

You declared the array to be of type String, so it performed an alphabetical
sort.

If you want it to sort numerically, you need to declare the array with a
numerical type, such as Integer, Double or Decimal.

HTH,

--

(O)enone
Author
9 Jul 2006 1:36 PM
R. MacDonald
Hello, Jack,

However, if you really NEED the contents of the ArrayList to be Strings,
you could create your own comparer class.  For example:

     Public Class NumericComparer
         Implements IComparer
         Public Function Compare(ByVal x As Object, _
                                 ByVal y As Object) As Integer _
                                     Implements IComparer.Compare
             Return CDbl(x) - CDbl(y)
         End Function
     End Class

Then replace your line:

     MyArrayList.Sort()

with

     Dim MyNumericComparer As New NumericComparer
     MyArrayList.Sort(MyNumericComparer)

I think that will give you what you're looking for.

Note that this trivial comparer will throw an exception if any of your
ArrayList elements cannot be converted to double.

Cheers,
Randy


Oenone wrote:
Show quoteHide quote
> Jack wrote:
>
>>I have an array that won't sort correctly:
>
> [...]
>
>>Dim MyString As String = "5,3,44,22,20,2,40,1,32.50"
>
> [...]
>
>>I get:
>>
>>1.00
>>2.00
>>20.00
>>22.00
>>3.00
>>32.50
>>40.00
>>44.00
>
>
> Looks like it's sorted perfectly to me.
>
> You declared the array to be of type String, so it performed an alphabetical
> sort.
>
> If you want it to sort numerically, you need to declare the array with a
> numerical type, such as Integer, Double or Decimal.
>
> HTH,
>
Author
11 Jul 2006 4:20 AM
Jack
Thanks you very much!!!

This is just what I needed.

Jack

Show quoteHide quote
"R. MacDonald" <sci***@NO-SP-AMcips.ca> wrote in message
news:44b10651$0$54997$dbd4f001@news.wanadoo.nl...
> Hello, Jack,
>
> However, if you really NEED the contents of the ArrayList to be Strings,
> you could create your own comparer class.  For example:
>
>     Public Class NumericComparer
>         Implements IComparer
>         Public Function Compare(ByVal x As Object, _
>                                 ByVal y As Object) As Integer _
>                                     Implements IComparer.Compare
>             Return CDbl(x) - CDbl(y)
>         End Function
>     End Class
>
> Then replace your line:
>
>     MyArrayList.Sort()
>
> with
>
>     Dim MyNumericComparer As New NumericComparer
>     MyArrayList.Sort(MyNumericComparer)
>
> I think that will give you what you're looking for.
>
> Note that this trivial comparer will throw an exception if any of your
> ArrayList elements cannot be converted to double.
>
> Cheers,
> Randy
>
>
> Oenone wrote:
>> Jack wrote:
>>
>>>I have an array that won't sort correctly:
>>
>> [...]
>>
>>>Dim MyString As String = "5,3,44,22,20,2,40,1,32.50"
>>
>> [...]
>>
>>>I get:
>>>
>>>1.00
>>>2.00
>>>20.00
>>>22.00
>>>3.00
>>>32.50
>>>40.00
>>>44.00
>>
>>
>> Looks like it's sorted perfectly to me.
>>
>> You declared the array to be of type String, so it performed an
>> alphabetical sort.
>>
>> If you want it to sort numerically, you need to declare the array with a
>> numerical type, such as Integer, Double or Decimal.
>>
>> HTH,
>>