55// Created by David Bureš - P on 29.04.2026.
66//
77
8+ import CorkShared
89import Foundation
910import SwiftUI
10- import CorkShared
1111
1212/// Adds support for parsing, storing and displaying a Brew package name in a friendly manner
1313public protocol PackageNameDisplayable
@@ -28,8 +28,50 @@ public protocol PackageNameDisplayable
2828}
2929
3030/// The package's name parsed into chunks
31- public struct BrewPackageName : Equatable , Hashable , Codable , Sendable
31+ public struct BrewPackageName : Equatable , Hashable , Codable , Comparable , Sendable
3232{
33+ public static func < ( lhs: BrewPackageName , rhs: BrewPackageName ) -> Bool
34+ {
35+ // First, compare by packageIdentifier alphabetically
36+ if lhs. packageIdentifier != rhs. packageIdentifier
37+ {
38+ return lhs. packageIdentifier < rhs. packageIdentifier
39+ }
40+
41+ // If identifiers are equal, handle boundVersion comparison
42+ switch ( lhs. boundVersion, rhs. boundVersion)
43+ {
44+ case ( nil , nil ) : // Both have no bound version — equal
45+ return false
46+
47+ case ( nil , _) : // lhs has no version, rhs does — lhs comes first
48+ return true
49+
50+ case ( _, nil ) : // rhs has no version, lhs does — rhs comes first
51+ return false
52+
53+ case ( let lhsVersion? , let rhsVersion? ) : // Both have versions
54+ // Check if both are purely numeric
55+ let lhsIsNumeric = Double ( lhsVersion) != nil
56+ let rhsIsNumeric = Double ( rhsVersion) != nil
57+
58+ switch ( lhsIsNumeric, rhsIsNumeric)
59+ {
60+ case ( false , false ) : // Both alphanumeric — sort alphabetically
61+ return lhsVersion < rhsVersion
62+
63+ case ( false , true ) : // lhs alphanumeric, rhs numeric — lhs comes first
64+ return true
65+
66+ case ( true , false ) : // lhs numeric, rhs alphanumeric — rhs comes first
67+ return false
68+
69+ case ( true , true ) : // Both numeric — sort numerically ascending
70+ return Int ( lhsVersion) ! < Int ( rhsVersion) !
71+ }
72+ }
73+ }
74+
3375 public init ( from unparsedName: String )
3476 {
3577 let packageNameWithoutTap : String =
@@ -134,7 +176,7 @@ public extension PackageNameDisplayable
134176 . foregroundColor ( . green)
135177 . font ( . subheadline)
136178 }
137-
179+
138180 if displayComponents. contains ( . boundVersion)
139181 {
140182 if let boundVersion = self . internalName. boundVersion
0 commit comments