3

Working with vb 2008 I need to sort an array of strings: (for example:)

dim list1() as String={"CONE0Z08TTTBALL","BARE0U04TTTBALL",  
             "APTN0S01TTTBALL","SPTN0K02TTTBALL"}

sorted from 5th character (not first):

  1. SPTN0K02TTTBALL
  2. VPTN0S01TTTBALL
  3. BARE0U04TTTBALL
  4. CONE0Z08TTTBALL

There is any way using the Array class sort?

3
  • SPTN0K02TTTBALL isnt in the array - is that a typo? Commented Jan 31, 2016 at 15:59
  • Repaired! copypaste error! Commented Jan 31, 2016 at 16:04
  • but now, VPTN0S01TTTBALL isnt in the list...I get the drift though Commented Jan 31, 2016 at 16:15

1 Answer 1

3

You can use linq to do that. I changed APTN0K02TTTBALL to SPTN0K02TTTBALL since that looks like a typo.

Dim list1() As String = {"CONE0Z08TTTBALL", "BARE0U04TTTBALL",
     "APTN0S01TTTBALL", "SPTN0K02TTTBALL"}
Dim result = list1.OrderBy(Function(q) q.Substring(5)).ToArray

For Each s As String In result
    Console.WriteLine(s)
Next

Output:

SPTN0K02TTTBALL
APTN0S01TTTBALL
BARE0U04TTTBALL
CONE0Z08TTTBALL

Note: If by 5th character, you meant 1-based, change the substring argument (which is 0 based) . Also, this is an alpha sort, so if you want to sort by the value of a numeral, you should also convert to integer. Not sure which you mean.

ryanyuyu kindly wrote you this DotNetFiddle

Sign up to request clarification or add additional context in comments.

3 Comments

So... as you were working on this answer, I was creating a demo. Since my code ended up being almost identical to yours, you can use it if desired. Demo
nice, I added it. I used to write lots of those, but looking at the view count it seems that VB people never look at them. Odd. @ryanyuyu
Thanks for your help. Numbers are treated as char, so I thing it will work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.