3 Replies - 547 Views - Last Post: 09 January 2017 - 08:52 AM Rate Topic: -----

#1 logreach  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-January 17

I am trying to sort a list, which contains size n the folder paths.

Posted 08 January 2017 - 02:05 PM

I am trying to sort a list, which contains size n the folder paths. I want to sort by size.
I used sort method but it looks like it doesn't work for a list. Can anyone help me here. Here is my code:
Do I need to use bubble sort here?
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim DirectoryList As New List(Of Details)
        
        Dim Dirs() As String = Directory.GetDirectories("C:\work")
        For Each Dir As String In Dirs
            GetDirectories(Dir, DirectoryList)
        Next
        DirListMaster.Sort()
        Debug.Print("Sorted:", DirListMaster(0))
    End Sub

    Public Sub GetDirectories(ByVal StartPath As String, ByRef DirectoryList As List(Of Details))
        Dim DirList As New List(Of Details)
        Dim totalfilesize As Long = 0
        Dim totaldirfilesize As Long = 0
       

            Dim Dirs() As String = Directory.GetDirectories(StartPath)
            For Each Dir As String In Dirs
                GetDirectories(Dir, DirectoryList)
            Next
            
            Dim mypath As DirectoryInfo = New DirectoryInfo(StartPath)
            Dim fis As FileInfo() = mypath.GetFiles()
            For Each myfile In fis
                totalfilesize = myfile.Length + totalfilesize
            Next
            
            mystruct.path = StartPath
            mystruct.size = totalfilesize
            DirList.Add(mystruct)

            DirListMaster.Add(mystruct)
            ListBox1.Items.Add(totalfilesize & vbTab & vbTab & StartPath)
        End Sub
:code:

This post has been edited by modi123_1: 08 January 2017 - 02:22 PM
Reason for edit:: In the future please use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: I am trying to sort a list, which contains size n the folder paths.

#2 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,674
  • Joined: 01-February 13

Re: I am trying to sort a list, which contains size n the folder paths.

Posted 08 January 2017 - 02:53 PM

You can use the Enumerable.OrderBy or Enumerable.OrderByDescending methods to sort a List(Of T) by a specific property or member of the Class or Structure types that the List holds. Below is a simple example that you can check out.
Public Class Form1
    Private Folders As New List(Of FolderInfo)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        GetAndSortDirectoriesBySize("C:\TestFolder")
        For Each Folder As FolderInfo In Folders
            ListBox1.Items.Add(Folder.TotalSizeInBytes.ToString & "   " & Folder.FolderPath)
        Next
    End Sub

    Private Sub GetAndSortDirectoriesBySize(BaseFolder As String)
        For Each di As IO.DirectoryInfo In New IO.DirectoryInfo(BaseFolder).GetDirectories 'iterate through the directories
            Dim TotalFolderSize As Long = 0

            'iterate through all files including ones in sub folders and add their sizes to the TotalFolderSize
            For Each fi As IO.FileInfo In di.GetFiles("*.*", IO.SearchOption.AllDirectories)
                TotalFolderSize += fi.Length
            Next

            'add a new FolderInfo class to the Folders list
            Folders.Add(New FolderInfo(di.FullName, TotalFolderSize))
        Next

        'Sort the Folders (FolderInfo) by their TotalSizeInBytes property
        Folders = Folders.OrderByDescending(Function(x) x.TotalSizeInBytes).ToList 'order from largest to smallest
        'Folders = Folders.OrderBy(Function(x) x.TotalSizeInBytes).ToList 'order from smallest to largest
    End Sub
End Class

Public Class FolderInfo
    Public Property FolderPath As String
    Public Property TotalSizeInBytes As Long
    Public Sub New(fPath As String, fSize As Long)
        FolderPath = fPath
        TotalSizeInBytes = fSize
    End Sub
End Class

Was This Post Helpful? 1
  • +
  • -

#3 logreach  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-January 17

Re: I am trying to sort a list, which contains size n the folder paths.

Posted 09 January 2017 - 08:33 AM

Thank you very much, it worked. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,674
  • Joined: 01-February 13

Re: I am trying to sort a list, which contains size n the folder paths.

Posted 09 January 2017 - 08:52 AM

You`re welcome. Glad i could help. 8)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1