Link to home
Start Free TrialLog in
Avatar of vb7guy
vb7guyFlag for United States of America

asked on

VB.Net Sort List(of Strings()) in Decending order

Hi, I have a project in VB.net Visual Studio 2005 where I grab a list of zip files from a folder and put them into two different List (of String()) arrays. These two arrays are based on file type, if file name contains word "_LD" then it goes into different array.  But both list arrays have 4 columns (Year,Quarter,UpdateDate,FileName),   I want to be able to sort my List arrays in so that  year column is in descending order but the quarter column in Ascending order. I don't want to rely on file system object to sort.


Zip file Format:
2008_3.zip
2008_2.zip
2008_1.zip
2008_4.zip
2010_1.zip
2010_3.zip
2010_4.zip
2010_2.zip
2012_2_LD.ZIP
2012_3_LD.ZIP
2012.4_LD.ZIP
2012_1_LD.ZIP
2010_2_LD.ZIP
2010_3_LD.ZIP
2010_4_LD.ZIP
2010_1_LD.ZIP



Private Sub cmdCreateZip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreateZip.Click

        Dim FileYYYY As String
        Dim Quarter As String
        Dim UpdateDate As String
        Dim ZipFileEntries As String()
        Dim ZipFileEntriesSorted As List(Of String())
        Dim LDZipFileEntriesSorted As List(Of String())
        Dim ZipFileName As String = ""

        ZipFileEntries = Directory.GetFiles("C:\ZipFilesTest\, "*.zip")
        ZipFileEntriesSorted = New List(Of String())
        LDZipFileEntriesSorted = New List(Of String())
        lstFiles.Sorted = False
        For Each f1 As String In ZipFileEntries
            ZipFileName = Path.GetFileNameWithoutExtension(f1)
            FileYYYY = Path.GetFileNameWithoutExtension(ZipFileName).Substring(0, 4)
            Quarter = Path.GetFileNameWithoutExtension(ZipFileName).Substring(5, 1)
            UpdateDate = File.GetCreationTime(f1).ToShortDateString
            If f1.Contains("_LD") = True Then
                LDZipFileEntriesSorted.Add(New String() {FileYYYY, Quarter, UpdateDate, Path.GetFileName(f1)})
            Else
                ZipFileEntriesSorted.Add(New String() {FileYYYY, Quarter, UpdateDate, Path.GetFileName(f1)})
            End If
        Next
    End Sub
Avatar of vb7guy
vb7guy
Flag of United States of America image

ASKER

Here is how one of the list array looks like.  Currently the array is in rendom order.

Year      Quarter      UpdateDate      FileName
1999      1      11/2/2012      1999_1.zip
1999      2      11/2/2012      1999_2.zip
1999      3      11/2/2012      1999_3.zip
1999      4      11/2/2012      1999_4.zip
2000      1      11/2/2012      2000_1.zip
2000      2      11/2/2012      2000_2.zip
2000      3      11/2/2012      2000_3.zip
2000      4      11/2/2012      2000_4.zip
2001      1      11/2/2012      2001_1.zip
2001      2      11/2/2012      2001_2.zip
2001      3      11/2/2012      2001_3.zip
2001      4      11/2/2012      2001_4.zip
2002      1      11/2/2012      2002_1.zip
2002      2      11/2/2012      2002_2.zip
2002      3      11/2/2012      2002_3.zip
2002      4      11/2/2012      2002_4.zip
2003      1      11/2/2012      2003_1.zip
2003      2      11/2/2012      2003_2.zip
2003      3      11/2/2012      2003_3.zip
2003      4      11/2/2012      2003_4.zip
2004      1      11/2/2012      2004_1.zip
2004      2      11/2/2012      2004_2.zip
2004      3      11/2/2012      2004_3.zip
2004      4      11/2/2012      2004_4.zip
2005      1      11/2/2012      2005_1.zip
2005      2      11/2/2012      2005_2.zip
2005      3      11/2/2012      2005_3.zip
2005      4      11/2/2012      2005_4.zip
2006      1      11/2/2012      2006_1.zip
2006      2      11/2/2012      2006_2.zip
2006      3      11/2/2012      2006_3.zip
2006      4      11/2/2012      2006_4.zip
2007      1      11/2/2012      2007_1.zip
2007      2      11/2/2012      2007_2.zip
2007      3      11/2/2012      2007_3.zip
2007      4      11/2/2012      2007_4.zip
2008      1      11/2/2012      2008_1.zip
2008      2      11/2/2012      2008_2.zip
2008      3      11/2/2012      2008_3.zip
2008      4      11/2/2012      2008_4.zip
2009      1      11/2/2012      2009_1.zip
2009      2      11/2/2012      2009_2.zip
2009      3      11/2/2012      2009_3.zip
2009      4      11/2/2012      2009_4.zip
2010      1      11/2/2012      2010_1.zip
2010      2      11/2/2012      2010_2.zip
2010      3      11/2/2012      2010_3.zip
2010      4      11/2/2012      2010_4.zip
2011      1      11/2/2012      2011_1.zip
2011      2      11/2/2012      2011_2.zip
2011      3      11/2/2012      2011_3.zip
2011      4      11/2/2012      2011_4.zip
2012      1      11/2/2012      2012_1.zip
2012      2      11/2/2012      2012_2.zip
2012      3      11/2/2012      2012_3.zip
2012      4      11/2/2012      2012_4.zip
Avatar of vb7guy

ASKER

And I want my array to look like this.  I don't know how to do this in Code. If possible I'd like to implement iComparer in .net 2005

Year      Quarter      UpdateDate      FileName
2012      1      11/2/2012      2012_1.zip
2012      2      11/2/2012      2012_2.zip
2012      3      11/2/2012      2012_3.zip
2012      4      11/2/2012      2012_4.zip
2011      1      11/2/2012      2011_1.zip
2011      2      11/2/2012      2011_2.zip
2011      3      11/2/2012      2011_3.zip
2011      4      11/2/2012      2011_4.zip
2010      1      11/2/2012      2010_1.zip
2010      2      11/2/2012      2010_2.zip
2010      3      11/2/2012      2010_3.zip
2010      4      11/2/2012      2010_4.zip
2009      1      11/2/2012      2009_1.zip
2009      2      11/2/2012      2009_2.zip
2009      3      11/2/2012      2009_3.zip
2009      4      11/2/2012      2009_4.zip
2008      1      11/2/2012      2008_1.zip
2008      2      11/2/2012      2008_2.zip
2008      3      11/2/2012      2008_3.zip
2008      4      11/2/2012      2008_4.zip
2007      1      11/2/2012      2007_1.zip
2007      2      11/2/2012      2007_2.zip
2007      3      11/2/2012      2007_3.zip
2007      4      11/2/2012      2007_4.zip
2006      1      11/2/2012      2006_1.zip
2006      2      11/2/2012      2006_2.zip
2006      3      11/2/2012      2006_3.zip
2006      4      11/2/2012      2006_4.zip
2005      1      11/2/2012      2005_1.zip
2005      2      11/2/2012      2005_2.zip
2005      3      11/2/2012      2005_3.zip
2005      4      11/2/2012      2005_4.zip
2004      1      11/2/2012      2004_1.zip
2004      2      11/2/2012      2004_2.zip
2004      3      11/2/2012      2004_3.zip
2004      4      11/2/2012      2004_4.zip
2003      1      11/2/2012      2003_1.zip
2003      2      11/2/2012      2003_2.zip
2003      3      11/2/2012      2003_3.zip
2003      4      11/2/2012      2003_4.zip
2002      1      11/2/2012      2002_1.zip
2002      2      11/2/2012      2002_2.zip
2002      3      11/2/2012      2002_3.zip
2002      4      11/2/2012      2002_4.zip
2001      1      11/2/2012      2001_1.zip
2001      2      11/2/2012      2001_2.zip
2001      3      11/2/2012      2001_3.zip
2001      4      11/2/2012      2001_4.zip
2000      1      11/2/2012      2000_1.zip
2000      2      11/2/2012      2000_2.zip
2000      3      11/2/2012      2000_3.zip
2000      4      11/2/2012      2000_4.zip
1999      1      11/2/2012      1999_1.zip
1999      2      11/2/2012      1999_2.zip
1999      3      11/2/2012      1999_3.zip
1999      4      11/2/2012      1999_4.zip
Avatar of vmoney502
vmoney502

This should answer you question

1999      1      11/2/2012      1999_1.zip
1999      2      11/2/2012      1999_2.zip
1999      3      11/2/2012      1999_3.zip
1999      4      11/2/2012      1999_4.zip
2000      1      11/2/2012      2000_1.zip
2000      2      11/2/2012      2000_2.zip
2000      3      11/2/2012      2000_3.zip
2000      4      11/2/2012      2000_4.zip
2001      1      11/2/2012      2001_1.zip
2001      2      11/2/2012      2001_2.zip
2001      3      11/2/2012      2001_3.zip
2001      4      11/2/2012      2001_4.zip
2002      1      11/2/2012      2002_1.zip
2002      2      11/2/2012      2002_2.zip
2002      3      11/2/2012      2002_3.zip
2002      4      11/2/2012      2002_4.zip
2003      1      11/2/2012      2003_1.zip
2003      2      11/2/2012      2003_2.zip
2003      3      11/2/2012      2003_3.zip
2003      4      11/2/2012      2003_4.zip
2004      1      11/2/2012      2004_1.zip
2004      2      11/2/2012      2004_2.zip
2004      3      11/2/2012      2004_3.zip
2004      4      11/2/2012      2004_4.zip
2005      1      11/2/2012      2005_1.zip
2005      2      11/2/2012      2005_2.zip
2005      3      11/2/2012      2005_3.zip
2005      4      11/2/2012      2005_4.zip
2006      1      11/2/2012      2006_1.zip
2006      2      11/2/2012      2006_2.zip
2006      3      11/2/2012      2006_3.zip
2006      4      11/2/2012      2006_4.zip
2007      1      11/2/2012      2007_1.zip
2007      2      11/2/2012      2007_2.zip
2007      3      11/2/2012      2007_3.zip
2007      4      11/2/2012      2007_4.zip
2008      1      11/2/2012      2008_1.zip
2008      2      11/2/2012      2008_2.zip
2008      3      11/2/2012      2008_3.zip
2008      4      11/2/2012      2008_4.zip
2009      1      11/2/2012      2009_1.zip
2009      2      11/2/2012      2009_2.zip
2009      3      11/2/2012      2009_3.zip
2009      4      11/2/2012      2009_4.zip
2010      1      11/2/2012      2010_1.zip
2010      2      11/2/2012      2010_2.zip
2010      3      11/2/2012      2010_3.zip
2010      4      11/2/2012      2010_4.zip
2011      1      11/2/2012      2011_1.zip
2011      2      11/2/2012      2011_2.zip
2011      3      11/2/2012      2011_3.zip
2011      4      11/2/2012      2011_4.zip
2012      1      11/2/2012      2012_1.zip
2012      2      11/2/2012      2012_2.zip
2012      3      11/2/2012      2012_3.zip
2012      4      11/2/2012      2012_4.zip
Avatar of vb7guy

ASKER

The sort list you send isn't correct.  I want the list to be sorted like this (Year descending and Quarter Ascending order)
Do you have sample code.
Avatar of Julian Hansen
Sounds like what you need is the array.Sort() method with a custom compare function.

http://msdn.microsoft.com/en-us/library/bzw8611x.aspx

In your compare function check if x.year = y.year and if so then compare the quarters and return < or > baseed on the quarter value.

If you need code post back.
Avatar of vb7guy

ASKER

If you have sample code that would be wonderful.  in the compare function X and Y would year. how do I pass in the quarter. by the way, My list is just generic list of array
SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vb7guy

ASKER

Thanks JulianH for pointing in the right direction. I have tweaked the FileComparer function a little bit. now it will work with List of string()

Imports System
Imports System.Collections.Generic
Public Class FileComparer
    Implements IComparer(Of String())

    Public Function Compare(ByVal x() As String, ByVal y() As String) As Integer Implements System.Collections.Generic.IComparer(Of String()).Compare
        'ByVal x As String, ByVal y As String) As Integer Implements IComparer(Of String).Compare

        'ByVal x() As String, ByVal y() As String) As Integer Implements System.Collections.Generic.IComparer(Of String()).Compare

        Dim rv As Integer
        Dim s1 As String
        Dim s2 As String
        s1 = x(0)
        s2 = y(0)

        's1 = Left(x, 4)
        's2 = Left(y, 4)
        rv = s2.CompareTo(s1)
        If rv = 0 Then
            s1 = x(1)
            s2 = y(1)
            's1 = Mid(x, 11, 1)
            's2 = Mid(y, 11, 1)
            rv = s1.CompareTo(s2)
        End If
        ' Compare y and x in reverse order.
        Return rv

    End Function
End Class
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vb7guy

ASKER

Both solutions were good.  I chose JulianH solution because it's using    Implements IComparer method. Thanks