?
Solved

Sort 2D array by multiple colums VB 2005

Posted on 2008-10-27
10
Medium Priority
?
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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