This is a pretty tough one in my opinion...

I have 4 parallel arrays, each increasing as lines are being read from a text file. The four arrays are booktitle, retail value, books on hand, and books on order. I have to find a way to sort all four arrays in ALPHABETICAL ORDER BY TITLE. So, for example, if the second element of the booktitle array was AAA, then that element AS WELL AS THE OTHER ARRAYS CORRESPONDING TO THAT ARRAY (AAA's retail value, AAA's on hand, AAA'S on order) need to be shifted to the first element position. I HAVE to use four parallel arrays so I cannot change that. Any ideas?

Here's the code:

Private Sub mnuItemOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuItemOpen.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

            myInFile = File.OpenText(OpenFileDialog1.FileName)
            If File.Exists(OpenFileDialog1.FileName) Then
                Do Until myInFile.Peek = -1
                    ReDim Preserve bookTitleArray(bookTitleCounter)
                    bookTitleArray(bookTitleCounter) = myInFile.ReadLine()
                    bookTitleCounter += 1
                    ReDim Preserve retailValueArray(retailValueCounter)
                    retailValueArray(retailValueCounter) = myInFile.ReadLine()
                    retailValueCounter += 1
                    ReDim Preserve booksHandArray(booksHandCounter)
                    booksHandArray(booksHandCounter) = myInFile.ReadLine()
                    booksHandCounter += 1
                    ReDim Preserve booksOrderArray(booksOrderCounter)
                    booksOrderArray(booksOrderCounter) = myInFile.ReadLine()
                    booksOrderCounter += 1
                Loop
            End If
        Else
            MessageBox.Show("File does not exist.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

Dani AI

Generated

For : the simplest robust pattern is to compute a permutation (index) that represents the sorted order of titles, then rebuild each of your four arrays in that order. That keeps your four parallel arrays intact, is O(n log n) for large lists, and avoids in-place swap bugs. Before sorting, verify all four arrays have the same length and normalize titles (Trim, consistent casing) so comparisons are reliable.

As demonstrated, using Array.Sort with auxiliary key arrays can work, but it requires careful copying and can be error-prone when titles repeat or when you need a stable result. The index-permutation approach is clearer and makes it easy to add a tie-breaker (original index) if you want stable ordering for equal titles.

Example (VB.NET) — build a sorted index, then copy into new arrays and reassign:

' build ordering by title (case-insensitive, trimmed)
Dim n = bookTitleArray.Length
Dim order = Enumerable.Range(0, n).
            OrderBy(Function(i) bookTitleArray(i).Trim().ToUpperInvariant()).
            ToArray()

Dim t(n - 1) As String, r(n - 1) As String, h(n - 1) As String, o(n - 1) As String
For dest As Integer = 0 To n - 1
    Dim src = order(dest)
    t(dest) = bookTitleArray(src)
    r(dest) = retailValueArray(src)
    h(dest) = booksHandArray(src)
    o(dest) = booksOrderArray(src)
Next

bookTitleArray = t
retailValueArray = r
booksHandArray = h
booksOrderArray = o

Troubleshooting tips: check for unequal lengths or stray blank lines when reading the file (off-by-one ReDim issues), handle Null/empty titles before comparing, and if you need culture-aware sorting use StringComparer.Create with the proper CultureInfo instead of ToUpperInvariant. If you must keep everything in-place, implement an index-based in-place cycle-walk using the permutation array to avoid extra allocation.

maybe not the best solution but a working one...

Dim arr1() As String = {"BAX", "XDS", "AAA", "GFD", "AAB"}
		Dim arr2() As String = {"67", "45", "2", "88", "89"}
		Dim arr3() As String = {"b4", "g5", "j3", "k8", "d3"}
		Dim arr4() As String = {"df", "hj", "tz", "we", "xs"}

		Dim count As Integer = arr1.GetUpperBound(0)
		
		Console.WriteLine("Original:")

		For x As Integer = 0 To count
			Console.WriteLine(String.Format("{0} - {1} - {2} - {3}", arr1(x), arr2(x), arr3(x), arr4(x)))
		Next x

		Dim arrkey(count) As String			  'hold original array
		Dim arrkey2(count) As String		  'hold original array
		Array.Copy(arr1, arrkey, count + 1)
		Array.Copy(arr1, arrkey2, count + 1)

		Array.Sort(arr1, arr2, 0, count + 1)
		Array.Sort(arrkey, arr3, 0, count + 1)
		Array.Sort(arrkey2, arr4, 0, count + 1)
		Console.WriteLine("Sorted:")

		For x As Integer = 0 To count
			Console.WriteLine(String.Format("{0} - {1} - {2} - {3}", arr1(x), arr2(x), arr3(x), arr4(x)))
		Next x
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.