Solved

Sort 2D array by multiple colums VB 2005

Posted on 2008-10-27
10
382 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
ID: 22811264
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
ID: 22811304
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
ID: 22811370
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:AlHal2
ID: 22811850
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
ID: 22813091
Numeric values uses "." and not comma ","!
If you build you own csv file you can use a different char as separater.
0
 

Author Comment

by:AlHal2
ID: 22814012
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
ID: 22815031
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
ID: 22815259
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
ID: 31510234
Thanks jpaulino
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22819887
Glad I could help.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

18 Experts available now in Live!

Get 1:1 Help Now