Results 1 to 10 of 10

Thread: Sorting items in an array.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    98

    Sorting items in an array.

    Say I have a demensional array, in the first row i have words, in the second one numbers, how can i sort the numbers from least to greatest?

  2. #2
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382

    Re: Sorting items in an array.

    Originally posted by Azkar
    Say I have a demensional array, in the first row i have words, in the second one numbers, how can i sort the numbers from least to greatest?
    You could make a binary heap, very fast result..

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    98
    eh? binary heap? is there a MSDN i read up on it?

  4. #4
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    Originally posted by Azkar
    eh? binary heap? is there a MSDN i read up on it?
    No a Binary Heap isn't a feature, it's a algorithm, it's simular to a bubble sort algorithm. Very easy to implement , I had to make one in a A* pathfinding algorithm I made.. just check here or you can check my code at PSC here and check out the portion of the code that uses a binary heap.

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Does the Array.Sort method support it?

  6. #6
    New Member skiman62's Avatar
    Join Date
    Nov 2003
    Posts
    12
    How about system.array.sort("arrayname")?

  7. #7
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs down

    System.Array.Sort(arSample) doesn't work: Only single dimension arrays are supported here.
    ~Peter


  8. #8
    New Member skiman62's Avatar
    Join Date
    Nov 2003
    Posts
    12
    Ohhhh those damn multi-dimensional arrays!!!!

  9. #9
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Wink

    But i tell you want. Here's some code for a BubbleSort. You just need to change it to work with multiple dimensions. It shouldn't take you more than a few minutes.
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim arSample(4) As String
            arSample(0) = "Jump"
            arSample(1) = "Free"
            arSample(2) = "Alpha"
            arSample(3) = "Zebra"
    
            arSample = BubbleSort(arSample)
            For iLpr As Integer = 0 To UBound(arSample) - 1
                ListBox1.Items.Add(arSample(iLpr))
            Next iLpr
    
            Beep()
        End Sub
    
        Public Function BubbleSort(ByVal arSort As Array) As Array
            'Programmed by MrGTI on 1999.10.13 and revised 2003.11.03
    
            'Gets upper bound of the sorting array (lower is always 0 in .NET)
            Dim iArrayTop As Integer = UBound(arSort)
    
            'Sorts array into alphabetical order
            For iOuterLoop As Integer = 0 To iArrayTop - 1  'Number of passes = # in array minus 1
                For iInnerLoop As Integer = 0 To iArrayTop - iOuterLoop
                    'This IF block allows the use of 0 in lBottom
                    If iOuterLoop = 0 And iInnerLoop = iArrayTop Then
                        'This is a single time when you don't want to check iInnerLoop + 1 (subscript out of range)
                    Else
                        'The AND statement stops the addition of a blank record
                        If arSort(iInnerLoop) > arSort(iInnerLoop + 1) And arSort(iInnerLoop + 1) > "" Then
                            Dim sTemp As String = arSort(iInnerLoop)
                            arSort(iInnerLoop) = arSort(iInnerLoop + 1)
                            arSort(iInnerLoop + 1) = sTemp
                        End If
                    End If
                Next iInnerLoop
            Next iOuterLoop
    
            Return arSort
        End Function
    ~Peter


  10. #10
    Fanatic Member
    Join Date
    Mar 2001
    Location
    Southern California
    Posts
    733
    This function has a Syntax error and I'm not exactly sure what it is.....any ideas anyone???

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width