Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do the items in an array or list maintain their order?

I am coding VB.NET in VS2008.

I have a comma delimited string of numbers, i.e. 16,7,99,1456,1,3

I do this in VB:

Dim MyArr() As String = MyString.Split(",")

Will MyArr keep the items in the order they were in the string?

If I do this:

For Each S as String in MyString.Split(",")
    'Do something with S
    'Will my items be in the same order they were
    'in the string?
Next

I tested it and it appears to keep the sort order but will it ~always~ keep the order?

If it does not maintain the order then what is a good way to split a string and keep order?

I'm asking because MSDN Array documentation says: "The Array is not guaranteed to be sorted." So I'm a bit unsure.

like image 374
rvarcher Avatar asked Jan 23 '23 21:01

rvarcher


2 Answers

Yes, in your example the items will stay in the original order.

The MSDN documentation indicates that an Array is not necessarily sorted just because it's an Array, but once the items are in the Array, they won't be rearranged. The Split() operation will break it down based on the given token, preserving the order.

like image 163
ahockley Avatar answered Jan 26 '23 09:01

ahockley


Yes, order will be maintained for these operations.

like image 43
Stephen Doyle Avatar answered Jan 26 '23 09:01

Stephen Doyle