Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

naturally orders files and folders #1714 #1774

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions CodeEdit/Utils/Extensions/Array/Array+SortURLs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,39 @@ extension Array where Element == URL {
/// Sorts the elements in alphabetical order.
/// - Parameter foldersOnTop: if set to `true` folders will always be on top of files.
/// - Returns: A sorted array of `URL`
func sortItems(foldersOnTop: Bool) -> Self {
var alphabetically = sorted { $0.lastPathComponent < $1.lastPathComponent }
func sortItems(foldersOnTop: Bool) -> [URL] {
return self.sorted { lhs, rhs in
let lhsIsDir = (try? lhs.resourceValues(forKeys: [.isDirectoryKey]).isDirectory) ?? false
let rhsIsDir = (try? rhs.resourceValues(forKeys: [.isDirectoryKey]).isDirectory) ?? false

if foldersOnTop {
var foldersOnTop = alphabetically.filter { $0.isFolder }
alphabetically.removeAll { $0.isFolder }
if foldersOnTop {
if lhsIsDir != rhsIsDir {
return lhsIsDir
}
}

foldersOnTop.append(contentsOf: alphabetically)
return compareNaturally(lhs.lastPathComponent, rhs.lastPathComponent)
}
}

return foldersOnTop
} else {
return alphabetically
/// Compare two strings using natural sorting.
/// - Parameters:
/// - lhs: The left-hand string.
/// - rhs: The right-hand string.
/// - Returns: `true` if `lhs` should be ordered before `rhs`.
private func compareNaturally(_ lhs: String, _ rhs: String) -> Bool {
let lhsComponents = lhs.components(separatedBy: CharacterSet.decimalDigits.inverted)
let rhsComponents = rhs.components(separatedBy: CharacterSet.decimalDigits.inverted)

for (lhsPart, rhsPart) in zip(lhsComponents, rhsComponents) where lhsPart != rhsPart {
if let lhsNum = Int(lhsPart), let rhsNum = Int(rhsPart) {
return lhsNum < rhsNum
} else {
return lhsPart < rhsPart
}
}

return lhs < rhs
}
}

Expand Down
Loading