NetNewsWire/Evergreen/Extensions/Node-Extensions.swift
2017-09-17 12:22:15 -07:00

52 lines
1.2 KiB
Swift

//
// Node-Extensions.swift
// Local
//
// Created by Brent Simmons on 8/10/16.
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import RSTree
import Data
extension Node {
class func nodesSortedAlphabetically(_ nodes: [Node]) -> [Node] {
return nodes.sorted { (node1, node2) -> Bool in
guard let obj1 = node1.representedObject as? DisplayNameProvider, let obj2 = node2.representedObject as? DisplayNameProvider else {
return false
}
let name1 = obj1.nameForDisplay
let name2 = obj2.nameForDisplay
return name1.localizedStandardCompare(name2) == .orderedAscending
}
}
class func nodesSortedAlphabeticallyWithFoldersAtEnd(_ nodes: [Node]) -> [Node] {
return nodes.sorted { (node1, node2) -> Bool in
if node1.canHaveChildNodes != node2.canHaveChildNodes {
if node1.canHaveChildNodes {
return false
}
return true
}
guard let obj1 = node1.representedObject as? DisplayNameProvider, let obj2 = node2.representedObject as? DisplayNameProvider else {
return false
}
let name1 = obj1.nameForDisplay
let name2 = obj2.nameForDisplay
return name1.localizedStandardCompare(name2) == .orderedAscending
}
}
}