Need to sort an Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jack

    #1

    Need to sort an Array

    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.Add Range(MyString .Split(","c))

    MyArrayList .Sort()

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

    Console.WriteLi ne(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

    #2
    Re: Need to sort an Array

    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.Add Range(MyString .Split(","c))
    >
    MyArrayList .Sort()
    >
    For i As Integer = 0 To MyArrayList .Count - 2
    >
    Console.WriteLi ne(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
    >

    Comment

    • Jack

      #3
      Re: Need to sort an Array

      Hello,

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

      Thanks,

      Jack
      "GhostInAK" <ghostinak@gmai l.comwrote in message
      news:c71747b427 ba38c870f2fa27e fb4@news.micros oft.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.Ad dRange(MyString .Split(","c))
      >>
      >MyArrayList .Sort()
      >>
      >For i As Integer = 0 To MyArrayList .Count - 2
      >>
      >Console.WriteL ine(FormatNumbe r(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
      >>
      >
      >

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Need to sort an Array

        "GhostInAK" <ghostinak@gmai l.comschrieb:
        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/>

        Comment

        • Oenone

          #5
          Re: Need to sort an Array

          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,

          --

          (O)enone


          Comment

          • R. MacDonald

            #6
            Re: Need to sort an Array

            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.Compa re
            Return CDbl(x) - CDbl(y)
            End Function
            End Class

            Then replace your line:

            MyArrayList.Sor t()

            with

            Dim MyNumericCompar er As New NumericComparer
            MyArrayList.Sor t(MyNumericComp arer)

            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,
            >

            Comment

            • Jack

              #7
              Re: Need to sort an Array

              Thanks you very much!!!

              This is just what I needed.

              Jack

              "R. MacDonald" <scitec@NO-SP-AMcips.cawrote in message
              news:44b10651$0 $54997$dbd4f001 @news.wanadoo.n l...
              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.Compa re
              Return CDbl(x) - CDbl(y)
              End Function
              End Class
              >
              Then replace your line:
              >
              MyArrayList.Sor t()
              >
              with
              >
              Dim MyNumericCompar er As New NumericComparer
              MyArrayList.Sor t(MyNumericComp arer)
              >
              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,
              >>

              Comment

              • GhostInAK

                #8
                Re: Need to sort an Array

                Hello Herfried K. Wagner [MVP],

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

                -Boo

                "GhostInAK" <ghostinak@gmai l.comschrieb:
                >
                >Array.Sort is a function, not a sub.
                >>
                'ArrayList.Sort ' is a sub.
                >

                Comment

                • GhostInAK

                  #9
                  Re: Need to sort an Array

                  Hello Herfried K. Wagner [MVP],

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

                  -Boo
                  "GhostInAK" <ghostinak@gmai l.comschrieb:
                  >
                  >Array.Sort is a function, not a sub.
                  >>
                  'ArrayList.Sort ' is a sub.
                  >

                  Comment

                  • GhostInAK

                    #10
                    Re: Need to sort an Array

                    Hello Herfried K. Wagner [MVP],

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

                    -Boo
                    "GhostInAK" <ghostinak@gmai l.comschrieb:
                    >
                    >Array.Sort is a function, not a sub.
                    >>
                    'ArrayList.Sort ' is a sub.
                    >

                    Comment

                    Working...