Solved

Sort 2D array by multiple colums VB 2005

Posted on 2008-10-27
10
385 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

695 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