|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Need to sort an ArrayI 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 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 > 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 >> > > "GhostInAK" <ghosti***@gmail.com> schrieb: 'ArrayList.Sort' is a sub.> Array.Sort is a function, not a sub. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> 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. > 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. > 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. > Jack wrote:
Show quoteHide quote > I have an array that won't sort correctly: Looks like it's sorted perfectly to me.[...] > 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 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 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, > 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, >>
Programmatically open compmgmt.msc
When the messagebox disappears, it leaves a white square Window Form refresh Windows Image Acquisition (WIA) Taking black photos Help with logic - system payment method Connect to database in non standard location .net remoting question Listview item fontstyle About download files vb express question |
|||||||||||||||||||||||