Swift array sorting VS java sorting (unknown cases management)

Comment from java, I try to find a way to sort an array in swift as I did with java. But after some tries, it looks like the behavior (not speaking about the API interface) is different.

In java I was doing something like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>myArray.sort( new Comparator<MyClass>() {
public int compare(MyClass obj1, MyClass obj2) {
... (return 1, -1, or zero)
}
}
</code>
<code>myArray.sort( new Comparator<MyClass>() { public int compare(MyClass obj1, MyClass obj2) { ... (return 1, -1, or zero) } } </code>
myArray.sort( new Comparator<MyClass>() {
    public int compare(MyClass obj1, MyClass obj2) {
        ... (return 1, -1, or zero)
    }
}

And if I returned zero, it was like “I don’t know, let’s see how it compare with other objects”. Meaning that zero does not means obj1 and obj2 are mandatorily together, but that the result of the comparison is undetermined.

In Swift, there is a simple syntax with:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>myArray.sort(by: {
... (return true or false)
})
</code>
<code>myArray.sort(by: { ... (return true or false) }) </code>
myArray.sort(by: {
    ... (return true or false)
})

But this implies that I’m always able to determine if the objects are > or <, which is not always the case.

Another syntax, more powerful, and that I was thinking similar to java, is

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>struct MySorter: SortComparator {
typealias Compared = MyStruct
var order: SortOrder = .forward
func compare(_ s1: MyStruct, _ s2: MyStruct) -> ComparisonResult {
... (return .orderedAscending, .orderedDescending, or .orderedSame)
}
}
</code>
<code>struct MySorter: SortComparator { typealias Compared = MyStruct var order: SortOrder = .forward func compare(_ s1: MyStruct, _ s2: MyStruct) -> ComparisonResult { ... (return .orderedAscending, .orderedDescending, or .orderedSame) } } </code>
struct MySorter: SortComparator {
    typealias Compared = MyStruct
    var order: SortOrder = .forward
    
    func compare(_ s1: MyStruct, _ s2: MyStruct) -> ComparisonResult {
        ... (return .orderedAscending, .orderedDescending, or .orderedSame)
    }
}

But here the .orderedSame seems to behave differently: when two objects are identified .orderedSame, they are considered being at the same level, and the sorter will no more try to compare them with other elements.

Is there something in Swift to get the same algo as in java, with the possibility to say “these two elements are not comparable” ?

A use case example is a case where having a set of object with dependencies (a tree), we have all nodes in an array and we want to order nodes depending on the dependencies (the root of the tree first, and then being sure that we never put a node A before another B if B depends on A.
For instance:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>struct Node {
let subNodes: [Node]
}
let node1_1 = Node(subNodes: [])
let node1_2 = Node(subNodes: [])
let node2_1 = Node(subNodes: [])
let node2_2 = Node(subNodes: [])
let node1 = Node(subNodes: [node1_1, node1_2])
let node2 = Node(subNodes: [node2_1, node2_2])
let nodeRoot = Node(subNodes: [node1, node2])
let allNodes: [Node] = [node2_2, node1_1, node1, node1_2, nodeRoot, node2_1, node2]
]
</code>
<code>struct Node { let subNodes: [Node] } let node1_1 = Node(subNodes: []) let node1_2 = Node(subNodes: []) let node2_1 = Node(subNodes: []) let node2_2 = Node(subNodes: []) let node1 = Node(subNodes: [node1_1, node1_2]) let node2 = Node(subNodes: [node2_1, node2_2]) let nodeRoot = Node(subNodes: [node1, node2]) let allNodes: [Node] = [node2_2, node1_1, node1, node1_2, nodeRoot, node2_1, node2] ] </code>
struct Node {
    let subNodes: [Node]
}

let node1_1 = Node(subNodes: [])
let node1_2 = Node(subNodes: [])
let node2_1 = Node(subNodes: [])
let node2_2 = Node(subNodes: [])
let node1 = Node(subNodes: [node1_1, node1_2])
let node2 = Node(subNodes: [node2_1, node2_2])
let nodeRoot = Node(subNodes: [node1, node2])
let allNodes: [Node] = [node2_2, node1_1, node1, node1_2, nodeRoot, node2_1, node2]

]

expecting:
nodeRoot, node1, node1_1, node1_2, node2, node2_1, node2_2
or
nodeRoot, node1, node2, node1_1, node1_2, node2_1, node2_2

but not
nodeRoot, node1, node2_2, node2, node1_1, node1_2, node2_1
(that I get currently in swift, because the sorter compare node1 & node2_2, found no point of comparison and returns .orderedSame, which then put them together what ever happen next.

I hope I’m clear
Thanks

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật