Click here to Skip to main content
15,895,817 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

I need to change the array item position in vb.net

{1,2,3,4} to {3,2,4,1}

Thanks,

Sivasankaran G
Posted

1 solution

VB
Public Shared Sub ChangeItemPos(ByVal a As Array)
    If (a IsNot Nothing) AndAlso (a.Length > 1) AndAlso (a.Rank = 1) Then
        Dim list As New ArrayList(a)
        Dim firstItem As Object = list.Item(0)
        list.RemoveAt(0)
        For index As Integer = 1 To list.Count - 1 Step 2
            ' Swap the items
            Dim temp As Object = list.Item(index)
            list.Item(index) = list.Item(index - 1)
            list.Item(index - 1) = temp
        Next
        list.Add(firstItem) ' Put firstItem into last
        list.CopyTo(a)
    End If
End Sub


Example:

C#
Dim a As Integer() = {1, 2, 3, 4}
ChangeItemPos(a)
 
Share this answer
 
Comments
Gssankar 17-Jan-13 6:08am    
Thanks for your response guna

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900