How to Sort String with Numeric values using VB Script?
Below are my strings from each row from a table:
- "Test 1 pass dec 2"
- "Test 3 fail"
- "Test 2 pass jun 4"
- "Verified"
- "Test 10 pass"
- "User Accepted"
I would to like get in below order after sorting(natural order):
- "Test 1 pass dec 2"
- "Test 2 pass jun 4"
- "Test 3 fail"
- "Test 10 pass"
- "User Accepted"
- "Verified"
Ways i have tried so far,
Set oAlist=CreateObject("System.Collections.ArrayList")
oAlist.sort
The ArrayList
was sorted in below order based on ASCII which I do not prefer:
- "Test 1 pass dec 2"
- "Test 10 pass"
- "Test 2 pass jun 4"
- "Test 3 fail"
- "User Accepted"
- "Verified"
I have tried this link Sort
and i have no idea how to use AppendFormat in my case.
Note:My given string either completely string or string with numbers(dynamic) so not sure how to use RecordSet or AppendFormat here as I am new to programming.
To apply the techniques from here to the problem (using Split instead of a RegExp):
output:
Just for fun: The 'same', but using a RegExp (better scaling technique):
You can have another example.
Since you are working with strings, you are going to need to write a custom sort function that can parse the test numbers from the strings.
Alternatively, you could pre-process your list and parse the numbers into a separate field, then sort based on that field.