Solved

Sort 2D array by multiple colums VB 2005

Posted on 2008-10-27
10
380 Views
Last Modified: 2012-05-05
Please take a look at the string sort in http://www.frez.co.uk/freecode.htm
This is written for VB6.  Can you upgrade to Vb 2005.
This only sorts 1D arrays.  I need to be able to sort 2D arrays.
My programs need to sort by multiple criteria.  I think that only merge sorts allow this.

I want to be able to call the routine like this.

Call MergeSortStringsStart(Array, Number of columns in the array, column by which to sort, True, True)
The 1st True means sort in ascending order.
The 2nd true means make it case sensitive
0
Comment
Question by:AlHal2
  • 5
  • 5
10 Comments
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
If you need to sort an array with two or more dimentions you can use insteade a datatable and use a dataview to sort like you want.
 
http://msdn.microsoft.com/en-us/library/system.data.dataview.sort.aspx
http://msdn.microsoft.com/en-us/library/system.data.dataview.aspx
0
 

Author Comment

by:AlHal2
Comment Utility
Thanks for this.

How do I get a csv (or whatever file I ingest) into a dataview.
One key identifier is a field that can be either numeric or alpha numeric.  In that case I would want to treat it as a string.  Would that be OK?
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
Comment Utility
You have here several example how to read a csv file to a datatable
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23344018.html
 
Then you just need (example) :

        ' Build a datatable with some record

        Dim dt As New DataTable("myTable")

        dt.Columns.Add("ID").AutoIncrement = True

        dt.Columns.Add("description")

        dt.Columns.Add("value")
 

        Dim rnd As New Random

        Dim dr As DataRow = Nothing

        For x As Byte = 0 To 49

            dr = dt.NewRow

            dr("description") = "Item " & x.ToString

            dr("value") = rnd.Next(0, 5)

            dt.Rows.Add(dr)

        Next
 

        ' sorts by the value

        Dim dv As New DataView(dt)

        dv.Sort = "value ASC"
 

        ' Show's the results

        For x As Integer = 0 To dv.Count - 1

            Dim result As String = String.Format("{0} - {1}", dv(x).Item("id"), dv(x).Item("value"))

            Debug.WriteLine(result)

        Next

Open in new window

0
 

Author Comment

by:AlHal2
Comment Utility
I looked at the accepted solution and saw this.

Dim input As String = reader.ReadLine()
        Dim fields As String() = input.Split(","c)
        Dim row As DataRow = table.NewRow()

This could cause a problem if one of the columns contains a comma within it.  When I do something like this in VB6 on a csv file I don't have a problem with commas in the middle of the text.  Perhaps it uses quote comma quote as a delimiter, but I'm not sure.  
I dimension an array as a string, but some columns in the csv file could be numeric and others could be actual strings.

Dim AryOld() As String, OldCt As Long, MaxOldCt As Long,Header as string

Open "C:\test.csv" For Input As #2
Line Input #2, Header
Do Until EOF(2)
    Input #2, AryOld(1, OldCt), AryOld(2, OldCt), Header, Header, AryOld(3, OldCt), Header, Header, _
      AryOld(4, OldCt), Header, Header, Header, Header, Header, Header, _
     Header, Header, AryOld(5, OldCt), Header, Header, Header, Header, _
        OldCt = OldCt + 1
        If OldCt = MaxOldCt Then
            MaxOldCt = MaxOldCt + 1000
            ReDim Preserve AryOld(1 To 5, 1 To MaxOldCt)
        End If
Loop
Close
ReDim Preserve AryOld(1 To 5, 1 To OldCt - 1)
0
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
Numeric values uses "." and not comma ","!
If you build you own csv file you can use a different char as separater.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:AlHal2
Comment Utility
Usually I'm given a csv file and have to work with it.  An example is where I have company names.  Sometimes people erroneously type a comma into the company name.  Then they list the company name with other fields eg its share price and SEDOL code  (SEDOL is a 7 digit alpha numeric figure identifying a company).
0
 

Author Comment

by:AlHal2
Comment Utility
I went back to the solution above and found this.  When I try to run it I get the message
"No value given for one or more required parameters."
when I come to the line "adapter.Fill(table)"
What's causing that message.
If it's fixed will I then have the data in a data table that I can sort as I wish?
Would I be able to print this table into another csv file?

Public Class Form1
    Dim dt As New DataTable
    Dim adapter As New SqlClient.SqlDataAdapter
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim path As String = "C:\temp\data.csv"

        Dim SQL As String = "SELECT *" & _
                            " FROM " & IO.Path.GetFileName(path).Replace(".", "#") & _
                            " WHERE Col5 > 0"

        Dim cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source='" & IO.Path.GetDirectoryName(path) & _
            "';Extended Properties=" & Chr(34) & _
            "Text;HDR=Yes;FMT=CSVDelimited" & Chr(34) & ";"

        Dim adapter As New OleDb.OleDbDataAdapter(SQL, cnString)

        Dim table As New DataTable()
        adapter.Fill(table)

        'View the contents of the file in your grid
        Me.DataGridView1.DataSource = table


    End Sub


End Class
0
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
If you want to work with a non regular csv file, you can use a SteamRead and do the split you want.

        Dim total As Integer = 0

        Dim fileName As String = "c:\teste.txt"

                                    

          Using reader As New IO.StreamReader(fileName)

                                    

         

            While Not reader.EndOfStream()

                                    

                Dim input As String = reader.ReadLine()

                Debug.WriteLine(input)

                total += 1

                                    

            End While

                                    

        End Using

Open in new window

0
 

Author Closing Comment

by:AlHal2
Comment Utility
Thanks jpaulino
0
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
Glad I could help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now