Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 3rd, 2004, 08:46 AM
Authorized User
 
Join Date: Jan 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Array Sorting

Hello there,
            I have a string array that contains numerical characters only. I was wondering if its possible to sort the array in order of ascending numbers of the numerical characters.

Thanks,
Dinesh


 
Old February 3rd, 2004, 10:41 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You'll have to write a sorting routine to do it.
 
Old February 3rd, 2004, 11:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jlick
Default

Here is a "Bubble" sort in VB6. I don't have VB.Net available, so I wrote it in VB6 and you will have to do the conversion. The bubble sort is fast for a small array. It will be slow for a large array. There are many sorts available, some are good for small arrays, and some are good for large arrays. As you can see, the worst case for this is to do N^2 calculations where N is the number of elements in the array. Some of the sorts that are better for large arrays will have a worst case of N*Log(N) (which is smaller than N^2).

Code:
Private Sub Command1_Click()
    Dim numberArray(9) As String
    Dim indexCounter As Long
    Dim bubbleCounter As Long
    Dim temporaryHolder As String

    ' Generate numbers.
    Call Randomize
    Debug.Print "Original Positions:"
    For indexCounter = 0 To 9
        numberArray(indexCounter) = CStr(Int(Rnd() * 100) + 1)
        Debug.Print indexCounter & vbTab & numberArray(indexCounter)
    Next

    ' Sort the array.
    For indexCounter = 1 To 9
        For bubbleCounter = indexCounter To 1 Step -1
            If Val(numberArray(bubbleCounter)) < Val(numberArray(bubbleCounter - 1)) Then
                temporaryHolder = numberArray(bubbleCounter - 1)
                numberArray(bubbleCounter - 1) = numberArray(bubbleCounter)
                numberArray(bubbleCounter) = temporaryHolder
            Else
                Exit For
            End If
        Next
    Next

    Debug.Print "Sorted:"
    For indexCounter = 0 To 9
        Debug.Print indexCounter & vbTab & numberArray(indexCounter)
    Next

End Sub
John R Lick
[email protected]





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting Array in C++ code_lover C++ Programming 4 October 16th, 2008 01:43 AM
Sorting 2 dimensional array rgalehouse Javascript How-To 2 December 2nd, 2006 10:46 AM
error when sorting an Array of Array nancy VBScript 2 February 17th, 2005 12:57 PM
sorting two dimensional array erin VBScript 0 February 10th, 2004 05:55 PM
Sorting an array roxusername PHP How-To 1 October 14th, 2003 01:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.