如何按百万像素然后按名称对列表进行排序?

How can I sort a List by megapixel and then by name?

前段时间,我在 VB.Net 中编写了一个程序,它类似于熟悉的 Windows 图片查看器,但有更多优点。今天我想进一步开发程序,另外还可以根据图像尺寸(即百万像素)进行排序,并且图像按此顺序(降序)显示。 所以我担心的是:我怎样才能先按百万像素排序,然后再按名称排序?我还不太擅长列表和它们的排序命令。 我写了一个单独的 class 称为“图像属性”。这里,对于每个图像,完整的路径和尺寸被传输并计算百万像素。

代码在Form_Main

Imports Microsoft.WindowsAPICodePack.Dialogs
Public NotInheritable Class Form_Main
    Private Shared Form2 As Form_Anzeige
    ''' <summary>
    ''' List with file names
    ''' </summary>
    Public Liste_mit_Dateinamen As List(Of String)
    Private ReadOnly Deu As New System.Globalization.CultureInfo("de-DE")
    ''' <summary>
    ''' List with instances of the class Bildeigenschaften (image properties)
    ''' </summary>
    Private Liste_Klasse_Bildeigenschaften As New List(Of Bildeigenschaften)
    Private Sub Form_Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Screens As Screen() = Screen.AllScreens
        If Screens.Length = 1 AndAlso (Screens(0).Bounds.Width < 1920 OrElse Screens(0).Bounds.Height < 1080) Then
            MessageBox.Show($"Es wurde kein zweiter Bildschirm angeschlossen und der erste hat eine zu kleine Auflösung{Environment.NewLine}(1920 × 1080 wäre nötig).", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return
        End If
    End Sub

    Private Sub Button_Start_Click(sender As Object, e As EventArgs) Handles Button_Start.Click
        Dim path_to_folder As String = ""
        Using OFolderD As New CommonOpenFileDialog
            OFolderD.Title = "Ordner auswählen"
            OFolderD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            OFolderD.IsFolderPicker = True
            If OFolderD.ShowDialog() = CommonFileDialogResult.Ok Then
                path_to_folder = OFolderD.FileName
            Else
                Return
            End If
        End Using

        Dim Liste_FI As New List(Of IO.FileInfo)
        Dim DI As IO.DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(path_to_folder)
        Dim ExtensionList As List(Of String) = New List(Of String) From {".jpg", ".jpeg", ".bmp", ".png"}

        For Each File As IO.FileInfo In DI.GetFiles
            If ExtensionList.Contains(File.Extension.ToLower(Deu)) Then
                Liste_FI.Add(File)
                Using temp_Bmp As New Bitmap(File.FullName)
                    Liste_Klasse_Bildeigenschaften.Add(New Bildeigenschaften(CUInt(temp_Bmp.Width), CUInt(temp_Bmp.Height), File.Name))
                End Using
            End If
        Next

        If Liste_FI.Count = 0 Then
            MessageBox.Show("In dem gewählten Ordner befinden sich keine Bilder (jpg, bmp, png).", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return
        End If

        Liste_mit_Dateinamen = New List(Of String)

        If RadioButton1.Checked Then
            Dim query As IEnumerable(Of IO.FileInfo) = Liste_FI.OrderBy(Function(Info As IO.FileInfo) Info.CreationTime)
            For Each fi As IO.FileInfo In query
                Liste_mit_Dateinamen.Add(fi.FullName)
            Next
        End If
        If RadioButton2.Checked Then
            Dim query As IEnumerable(Of IO.FileInfo) = Liste_FI.OrderBy(Function(Info As IO.FileInfo) Info.Name)
            For Each fi As IO.FileInfo In query
                Liste_mit_Dateinamen.Add(fi.FullName)
            Next
        End If
        If RadioButton3.Checked Then
'              here
        End If

        'Nicht-modaler Dialog auf großem Bildschirm (mind. 1920 × 1080). Benutzer soll Form2 nach Belieben (irgendwann) wegklicken.
        'non-modal dialog on large screen (at least 1920 × 1080). User can close Form2 at will (at some point). 
        Form2 = New Form_Anzeige
        If Not Form2.Visible AndAlso Not Form2.IsDisposed AndAlso Liste_mit_Dateinamen.Count > 0 Then
            Form2.Show(Me)
            Button_Start.Enabled = False
        
        End If
    End Sub

    Private Sub Button_Stop_Click(sender As Object, e As EventArgs) Handles Button_Stop.Click
        If Form2 IsNot Nothing Then
            Form2.Timer1.Stop()
        End If
    End Sub
End Class

class“Bildeigenschaften”(“图像属性”)代码

NotInheritable Class Bildeigenschaften
    Private Megapixels As UInt32
    Private Path_to_the_file As String = ""
    Public Sub New(ByVal _width As UInt32, ByVal _height As UInt32, ByVal _path As String)
        Me.Megapixels = _width * _height
        Me.Path_to_the_file = _path
    End Sub
End Class

例如

Public Class Thing

    Public Property First As String
    Public Property Second As String

End Class
Dim things As New List(Of Thing)

'...

things.Sort(Function(t1, t2)
                Dim result = t1.First.CompareTo(t2.First)

                If result = 0 Then
                    result = t1.Second.CompareTo(t2.Second)
                End If

                Return result
            End Function)

该代码使用 List(Of T).Sort(Comparison(Of T)) 方法。还有其他特定的排序方法,但它们都依赖于类似的原则,即重复比较成对的项目以产生表示它们相对大小的 Integer 值。在这样的比较中,小于零的值意味着第一项小于第一项,大于零的值意味着第二项小于第一项,零意味着它们相等。这就是为什么在这段代码中,当且仅当第一个 属性 的比较结果为零时,第二个 属性 才会被比较。

更多比较和排序方法请看我的博客posthere。请注意,post.

分为三个部分