Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 593 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extending Array to check if it is sorted in Swift?

#11
<blink>
Adaptation, a [solution][1] that will work in Swift 4
[1]:

[To see links please register here]


</blink>

extension Array where Iterator.Element: Comparable {
func isSorted(isOrderedBefore: (Iterator.Element, Iterator.Element) -> Bool) -> Bool {
for i in 1 ..< self.count {
if isOrderedBefore(self[i], self[i-1]) {
return false
}
}
return true
}
}
Reply

#12
The most flexible solution to me is a combination of NSAddict's and Wes Campaigne's answer. I.e. combine the advantage of being able to extend protocols and to pass comparator functions as arguments. This eliminates the restrictions both to use it only with arrays and to constrain it to elements conforming to `Comparable` protocol.

extension CollectionType
{
func isSorted(isOrderedBefore: (Generator.Element, Generator.Element) -> Bool) -> Bool
{
var previousIndex = startIndex
var currentIndex = startIndex.successor()

while currentIndex != endIndex
{
if isOrderedBefore(self[previousIndex], self[currentIndex]) == false
{
return false
}

previousIndex = currentIndex
currentIndex = currentIndex.successor()
}

return true
}
}

This can be used on any `Collection` type and sorting criteria can be defined according to your needs.
Reply

#13
In Swift 2.0 you can now extend protocols!

extension CollectionType where Generator.Element: Comparable {

public var isSorted: Bool {

var previousIndex = startIndex
var currentIndex = startIndex.successor()

while currentIndex != endIndex {

if self[previousIndex] > self[currentIndex] {
return false
}

previousIndex = currentIndex
currentIndex = currentIndex.successor()
}

return true
}

}

[1, 2, 3, 4].isSorted // true
["a", "b", "c", "e"].isSorted // true
["b", "a", "c", "e"].isSorted // false
[/* Anything not implementing `Comparable` */].isSorted // <~~ Type-error


Note that because we're using `Indexable.Index` instead of a simple `Int` as an index we have to use a while-loop instead, which looks a bit less pretty and clear.
Reply

#14
The alternative solution to a free function is to do what Swift's built-in `Array.sort` and `Array.sorted` methods do, and require that you pass a suitable comparator to the method:

extension Array {
func isSorted(isOrderedBefore: (T, T) -> Bool) -> Bool {
for i in 1..<self.count {
if !isOrderedBefore(self[i-1], self[i]) {
return false
}
}
return true
}
}

[1, 5, 3].isSorted(<) // false
[1, 5, 10].isSorted(<) // true
[3.5, 2.1, -5.4].isSorted(>) // true

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through