Am doing an application to allocate the seats to the students in a building with rooms. I have almost done to allocate the students appropriatly in the rooms. The problem now is convert them to a single variable. So here is the place am struck with:

I have these data in a rich textbox:

1   11  200
1   12  201
1   13  202
2   11  203
2   12  204
2   13  205
1   21  100
1   22  101
1   23  102
2   21  103
2   22  104
2   23  105
3   21  106

now I need the string as follows:

Variable 1: (string type)
    200,100,201,101,202,103

Variable 2: (string type)
    203,103,204,104,205,105

Variable 3: (string type)
    106

Its not really clear if you want 1 var or 3 var.You always can put them into an array:
arr1(6) as string =New String(){"200","100"......} and so on

Edited 2 Years Ago by Minimalist

Sorry a correction in the question:
Actually it should be:

Variable 1: (string type)
    200,100,201,101,202,**102**
Variable 2: (string type)
    203,103,204,104,205,105
Variable 3: (string type)
    106

From the above data:

  1. Rearranging 1st column
    step1:[make 1's together]
    1 11 200
    1 12 201
    1 13 202
    1 21 100
    1 22 101
    1 23 102

step2:[2's]
2 11 203
2 12 204
2 13 205
2 21 103
2 22 104
2 23 105

step3:[3's]
3 21 106

  1. Rearranging 2nd column
    step1:
    1 11 200
    1 21 100
    1 12 201
    1 22 101
    1 13 202
    1 23 102

similarly for 2's and 3's...

I see what you want (at least for the given numbers) but I still can't help with an algorithm without knowing more about the numbers. I presume column 3 is room numbers. What are the first two columns and what is the range of values? For example, by throwing away the leftmost digit of column 2, the sorting becomes obvious but this might not work for all values of column 2. Could you explain (in words) what you are trying to do?

An easy way to sort on multiple columns is to use a detached recordset. For your given data (I'm assuming columns 1 & 2 are consistently one and two digits respectively) I can build a recordset in which columns 1-3 are the same as your columns, and column 4 is the rightmost digit of your column 2. In that case we get

Imports ADODB

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'declare a detached recordset

        Dim mydata As New ADODB.Recordset
        mydata.CursorLocation = CursorLocationEnum.adUseClient
        mydata.LockType = LockTypeEnum.adLockBatchOptimistic
        mydata.CursorType = CursorTypeEnum.adOpenStatic
        mydata.ActiveConnection = Nothing

        'Add one field for each field of data plus one for sorting

        mydata.Fields.Append("col1", DataTypeEnum.adChar, 1)
        mydata.Fields.Append("col2", DataTypeEnum.adChar, 2)
        mydata.Fields.Append("col3", DataTypeEnum.adChar, 3)
        mydata.Fields.Append("col4", DataTypeEnum.adChar, 1)

        mydata.Open()

        'add the data from the richtextbox

        For Each line As String In RichTextBox1.Lines

            Dim flds() As String = line.Split()

            If UBound(flds) = 2 Then
                mydata.AddNew()
                mydata("col1").Value = flds(0)
                mydata("col2").Value = flds(1)
                mydata("col3").Value = flds(2)
                mydata("col4").Value = flds(1).Substring(1)
                mydata.Update()
            End If

        Next

        mydata.Sort = "col1, col4"

        mydata.MoveFirst()

        Dim c1old As String = ""
        Dim c1new As String = ""

        Do Until mydata.EOF
            c1new = mydata("col1").Value
            If c1new <> c1old Then
                If c1old <> "" Then TextBox1.AppendText(vbCrLf)
                TextBox1.AppendText(c1new & " " & mydata("col3").Value)
                c1old = c1new
            Else
                TextBox1.AppendText(" " & mydata("col3").Value)
            End If
            mydata.MoveNext()
        Loop

        TextBox1.AppendText(vbCrLf)
        mydata.Close()

    End Sub

End Class

I have made the assumption that the fields in the richtextbox are separated by exactly one blank. To use ADODB you must add a reference to adodb (it's under the .NET column in the add reference dialog). The value of TextBox1.Text after processing is

1 200 100 201 101 202 102
2 203 103 204 104 205 105
3 106

Edited 2 Years Ago by Reverend Jim

This article has been dead for over six months. Start a new discussion instead.