Solved

Sort 2D array by multiple colums VB 2005

Posted on 2008-10-27
10
383 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 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