Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I sort an array of strings that the last character is number accorcding to the numbers????? 3

Status
Not open for further replies.

WomanPro

Programmer
Nov 1, 2012
180
0
0
GR
Dim strNames(5) As String
Dim i As Integer
strNames(0) = "Button7"
strNames(1) = "Button15"
strNames(2) = "Button14"
strNames(3) = "Button13"
strNames(4) = "Button12"
strNames(5) = "Button3"

How can I sort this array to this with sort method???? Is there a way?????

strNames(0)="Button3"
strNames(1)="Button7"
strNames(2)="Button12"
strNames(3)="Button13"
strNames(4)="Button14"
strNames(5)="Button15"

I have trying the following with no effect. Any help please???? Any help will be much appreciated.

Dim strNames(5) As String
Dim indx As Byte
Dim i, j As Integer

strNames(0) = "Button7"
strNames(1) = "Button15"
strNames(2) = "Button14"
strNames(3) = "Button13"
strNames(4) = "Button12"
strNames(5) = "Button3"

Dim str, strName as string

For i = 0 To strNames.Length - 1
If strNames(i).Length > 7 Then
str = strNames(i).Substring(strNames(i).Length - 2, 2)
indx = 2
ElseIf strNames(i).Length = 7 Then
str = strNames(i).Substring(strNames(i).Length - 1)
indx = 1
End If
For j = i + 1 To strNames.Length - 1
If strNames(j).Length > 7 Then
str = strNames(j).Substring(strNames(j).Length - 2, 2)
indx = 2
ElseIf strNames(j).Length = 7 Then
str = strNames(j).Substring(strNames(j).Length - 1)
indx = 1
End If
If Val(strNames(i)) > Val(strNames(j)) Then
strName = strNames(i).Substring(0, strNames(i).Length - indx) & str
strNames(i) = strNames(i).Substring(0, strNames(i).Length - indx) & str
strNames(j) = strName
End If
Next
Next

For i = 0 To strNames.Length - 1
Console.WriteLine(strNames(i))
Next

Thank you so much
in advanced.
 
Is that your desired outcome?

strNames(0)="Button3"
strNames(1)="Button7"
strNames(2)="Button12"
strNames(3)="Button13"
strNames(4)="Button14"
strNames(5)="Button15"

If so, consider:

strNames(0)="Button[red]0[/red]3"
strNames(1)="Button[red]0[/red]7"
strNames(2)="Button12"
strNames(3)="Button13"
strNames(4)="Button14"
strNames(5)="Button15"

Unless you are willing to ignore the word 'Button' and format the number to be 2 digits (as text)

Have fun.

---- Andy
 
Thank you Andy!!!
It's not a very good idea to rename it like Button03 etc for example because all the button are creating at runtime. They are 15 buttons and they take their name like the following. It would affect all the application.
For i = 0 To UsrCheckers.Length - 1
UsrCheckerBtns(i).Name = "UsrCheckerBtn" & cnt
next

My code as I mentioned before doesn't work... :(
I saw effect only with a following, isolating the numeric chars stored into another array and reset them sorted in the initial array. However, I would like to give it a try with the previous code. In case I have no any effect, which is your opinion about the following???
Dim TempStrNames(5) As String

Dim indx As Byte
For i = 0 To strNames.Length - 1
If strNames(i).Length > 7 Then
TempStrNames(i) = strNames(i).Substring(strNames(i).Length - 2, 2)
indx = 2
ElseIf strNames(i).Length = 7 Then
TempStrNames(i) = strNames(i).Substring(strNames(i).Length - 1)
indx = 1
End If
Next
For i = 0 To TempStrNames.Length - 1
For j = i + 1 To TempStrNames.Length - 1
If Val(TempStrNames(i)) > Val(TempStrNames(j)) Then
strName = Val(TempStrNames(i))
TempStrNames(i) = TempStrNames(j)
TempStrNames(j) = strName
End If
Next
Next

For i = 0 To strNames.Length - 1
strNames(i) = ""
strNames(i) = "Button" & TempStrNames(i).ToString
Next
 
Well, this is just my opinion, but you can do this:

Code:
For i = 0 To UsrCheckers.Length - 1
  UsrCheckerBtns(i).Name = "UsrCheckerBtn" & [blue]Strings.Format([/blue]cnt[blue], "00")[/blue]
next

Have fun.

---- Andy
 

Instead of having to get the button's number from the button'n name, you could store an index number in the button's Tag property as you create it:

For i = 0 To UsrCheckers.Length - 1
UsrCheckerBtns(i).Name = "UsrCheckerBtn" & cnt
UsrCheckerBtns(i).Tag = cnt
Next

Then you can sort them using the Tag value.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Both solutions great!!!!! :)
Jebenson is it possible to sort them by tag????
The what I am trying to do is to sort button in some stacks.
For instance a stack with these elements. Each element is a Button!!!! Is it possible to sort them by tag property ?
UsrCheckerBtn7
UsrCheckerBtn15
UsrCheckerBtn14
UsrCheckerBtn13
UsrCheckerBtn12
UsrCheckerBtn3


I would like that effect of sorting
UsrCheckerBtn3
UsrCheckerBtn7
UsrCheckerBtn12
UsrCheckerBtn13
UsrCheckerBtn14
UsrCheckerBtn15
 
Solved with that example!!! Thank you so much both of you!!!
2 easy ways for that!!!! OMG!!!! Respect to the programmers!!!!!!

For i = 0 To btns.Length - 1
btns(i) = New Button
Next
btns(0).Name = "Btn7"
btns(0).Tag = 7
btns(1).Name = "Btn15"
btns(1).Tag = 15
btns(2).Name = "Btn14"
btns(2).Tag = 14
btns(3).Name = "Btn13"
btns(3).Tag = 13
btns(4).Name = "Btn12"
btns(4).Tag = 12
btns(5).Name = "Btn3"
btns(5).Tag = 3
Dim btn As New Button
For i = 0 To btns.Length - 1
For j As Byte = i + 1 To btns.Length - 1
If btns(i).Tag > btns(j).Tag Then
btn = btns(i)
btns(i) = btns(j)
btns(j) = btn
End If
Next
Next

For i = 0 To btns.Length - 1
MsgBox(btns(i).Name)
Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top