Swift sort array of objects based on boolean value - arrays

I'm looking for a way to sort a Swift array based on a Boolean value.
I've got it working using a cast to NSArray:
var boolSort = NSSortDescriptor(key: "selected", ascending: false)
var array = NSArray(array: results)
return array.sortedArrayUsingDescriptors([boolSort]) as! [VDLProfile]
But I'm looking for the Swift variant, any ideas?
Update
Thanks to Arkku, I've managed to fix this using the following code:
return results.sorted({ (leftProfile, rightProfile) -> Bool in
return leftProfile.selected == true && rightProfile.selected != true
})

Swift's arrays can be sorted in place with sort or to a new array with sorted. The single parameter of either function is a closure taking two elements and returning true if and only if the first is ordered before the second. The shortest way to use the closure's parameters is by referring to them as $0 and $1.
For example (to sort the true booleans first):
// In-place:
array.sort { $0.selected && !$1.selected }
// To a new array:
array.sorted { $0.selected && !$1.selected }
(edit: Updated for Swift 3, 4 and 5, previously sort was sortInPlace and sorted was sort.)

New (for Swift 1.2)
return results.sort { $0.selected && !$1.selected }
Old (for Swift 1.0)
Assuming results is of type [VDLProfile] and VDLProfile has a Bool member selected:
return results.sorted { $0.selected < $1.selected }
See documentation for sorted

Swift’s standard library provides a function called sorted, which sorts an array of values of a known type, based on the output of a sorting closure that you provide
reversed = sorted(array) { $0 > $1 }
reversed will be a new array which will be sorted according to the condition given in the closure.

Related

Array sorting error: " Binary operator '<' cannot be applied to two 'Int?' operands"

This is the code for sorting an array by the each cells in the tableView's timestamp.
self.ProjectsArray.sorted(by: { (project, project2) -> Bool in
return project.timestamp?.intValue < project2.timestamp?.intValue
})
Is there a better way to sort an array? What am I'm doing wrong?
EDIT- According to your comments you want to sort in place, so I am updating to sort in place.
Your timestamp variable is an Optional, so you may be comparing nil to nil, or nil to an Int. You can either unwrap these safely and provide a sort order in the case that one is nil, or you can use a nil-coalescing operator to treat nil value as some default Int like 0. The two options look like this:
Optional unwrapping:
self.ProjectsArray.sort(by: { (project, project2) -> Bool in
if let timestamp1 = project.timestamp, let timestamp2 = project2.timestamp {
return timestamp1.intValue < timestamp2.intValue
} else {
//At least one of your timestamps is nil. You have to decide how to sort here.
return true
}
})
Nil-coalescing operators:
self.ProjectsArray.sort(by: { (project, project2) -> Bool in
//Treat nil values as 0s and sort accordingly
return (project.timestamp?.intValue ?? 0) < (project2.timestamp?.intValue ?? 0)
})

Check whether integers in array are consecutive or in sequence

Is there any best method to check if elements in array are in consecutive order?
Eg:
[1,2,3,4,5] // returns true
[1,2,4,3,5] // returns false
Currently what I implement is to take difference of elements and if the diff is 1 then I say it is in consecutive order.
I'm looking for any improved approach. I think of adding extension to Array but not sure how to implement this.
Given your array
let list = [1,2,3,4,5]
you can use some Functional Programming magic
let consecutives = list.map { $0 - 1 }.dropFirst() == list.dropLast()
If this is a one-off question, then any little for-loop is fine, but it's an interesting problem to explore generic solutions. First, I'm assuming you mean that each element must be one greater than the one before, not just in order.
Let's build a generic way to answer "do all pairs of elements in this collection obey some rule." First, it'd be really nice to have a generic way to say "do all?"
extension Sequence {
func all(pass predicate: (Element) -> Bool) -> Bool {
// If nothing is false, everything is true
return !self.contains(where: { !predicate($0) })
}
}
This returns whether all elements of a sequence obey some rule.
Now we can ask the question: do all pair-wise elements of a collection obey some rule:
extension Collection {
func passesForConsecutiveValues(_ predicate:(Element, Element) -> Bool) -> Bool {
return zip(self, dropFirst()).all(pass: predicate)
}
}
zip(x, x.dropFirst() just creates "pair-wise elements", and then we ask "do they all satisfy our rule?" For example:
// Are all elements one more than their predecessor?
[1,2,4,5].passesForConsecutiveValues { $1 == $0 + 1 } // true
Now you may have noticed that I switched from Sequence to Collection in the middle there. Why? Because zip(x, x.dropFirst()) isn't defined on arbitrary sequences. You may only be allowed to iterate over a sequence once. Unfortunately there's no way to know; it's considered "special knowledge about the sequence" in the docs. Bleh. I miss Scala's TraversableOnce vs. Sequence that moves the requirement into the type.
That said, we absolutely can build this for Sequence. We just have to build a replacement for zip(x, x.dropFirst()). We'll call it pairwise and it'll return an iterator:
extension Sequence {
func pairwise() -> AnyIterator<(Element, Element)> {
var it = makeIterator()
guard var last_value = it.next() else { return AnyIterator{ return nil } }
return AnyIterator {
guard let value = it.next() else { return nil }
defer { last_value = value }
return (last_value, value)
}
}
}
And with that, we can build this on Sequence:
extension Sequence {
func passesForConsecutiveValues(_ predicate:(Element, Element) -> Bool) -> Bool {
return pairwise().all(pass: predicate)
}
}
"Currently what I implement is to take difference of elements and if
the diff is 1 then I say it is in sequence."
Based on your statement above, it seems your want to, for an array of integers, see if all members are consecutive.
You've already described the logic for this algorithm: you could implement it e.g. using a for ... in ... where loop, with a body which is only ever entered when the where clause identifies two subsequent elements which are not in consecutive order. E.g.:
extension Array where Element == Int {
func numbersAreConsecutive() -> Bool {
for (num, nextNum) in zip(self, dropFirst())
where (nextNum - num) != 1 { return false }
return true
}
}
var arr = [1, 2, 3, 4, 5]
print(arr.numbersAreConsecutive()) // true
arr = [1, 2, 4, 5]
print(arr.numbersAreConsecutive()) // false
arr = [1]
print(arr.numbersAreConsecutive()) // true
arr = []
print(arr.numbersAreConsecutive()) // true
arr = [2, 1]
print(arr.numbersAreConsecutive()) // false
Extending the extension to all types conforming to Integer:
extension Array where Element: Integer {
func numbersAreConsecutive() -> Bool {
for (num, nextNum) in zip(self, dropFirst())
where (nextNum - num) != 1 { return false }
return true
}
}
It will return the true if the sequence is expected otherwise it will return the false
It has two check
1.Checking whether the array is sequence(Find the array is sequence)
1.1 Sortedarray[0] + arraycount multiple with sequence (1,2,3, etc) and minus the sequence.
1.2 compare the above calculated value with last value of sorted array. if it matche we could consider The array is sequence.
2. Compare the source array and sorted array to confirm it is in order
isSeq([4,5,6,7],sequence:1) **return True**
isSeq([100,102,104,106,108],sequence:2) **return True**
isSeq([100,103,106,109,110],sequence:3) **return false**
func isSeq(_ arrayValue:[Int],sequence:Int) ->Bool{
let sortedValue = arrayValue.sorted()
if(sortedValue[0] + (sortedValue.count * sequence) - sequence == sortedValue[sortedValue.count - 1]){
if(arrayValue == sortedValue){
return true
}
}
return false;
}

Remove specific object from array in swift 3

I have a problem trying to remove a specific object from an array in Swift 3. I want to remove item from an array as in the screenshot but I don't know the solution.
If you have any solutions please share with me.
Short Answer
you can find the index of object in array then remove it with index.
var array = [1, 2, 3, 4, 5, 6, 7]
var itemToRemove = 4
if let index = array.index(of: itemToRemove) {
array.remove(at: index)
}
Long Answer
if your array elements confirm to Hashable protocol you can use
array.index(of: itemToRemove)
because Swift can find the index by checking hashValue of array elements.
but if your elements doesn't confirm to Hashable protocol or you don't want find index base on hashValue then you should tell index method how to find the item. so you use index(where: ) instead which asks you to give a predicate clouser to find right element
// just a struct which doesn't confirm to Hashable
struct Item {
let value: Int
}
// item that needs to be removed from array
let itemToRemove = Item(value: 4)
// finding index using index(where:) method
if let index = array.index(where: { $0.value == itemToRemove.value }) {
// removing item
array.remove(at: index)
}
if you are using index(where:) method in lots of places you can define a predicate function and pass it to index(where:)
// predicate function for items
func itemPredicate(item: Item) -> Bool {
return item.value == itemToRemove.value
}
if let index = array.index(where: itemPredicate) {
array.remove(at: index)
}
for more info please read Apple's developer documents:
index(where:)
index(of:)
According to your code, the improvement could be like this:
if let index = arrPickerData.index(where: { $0.tag == pickerViewTag }) {
arrPickerData.remove(at: index)
//continue do: arrPickerData.append(...)
}
The index existing means Array contains the object with that Tag.
I used the solutions provided here: Remove Specific Array Element, Equal to String - Swift Ask Question
this is one of the solutions there (in case the object was a string):
myArrayOfStrings = ["Hello","Playground","World"]
myArrayOfStrings = myArrayOfStrings.filter{$0 != "Hello"}
print(myArrayOfStrings) // "[Playground, World]"

When to use enumerate for arrays in Swift?

I noticed strange things happen when you try to remove or mutate array items in a loop. However, when doing it while calling enumerate() on the array, it works as expected. What is the concept behind it and when should we use enumerate()?
To answer the question from the title, you use enumerate() when you need value's index in addition to the value itself:
If you need the integer index of each item as well as its value, use the enumerate() method to iterate over the array instead.
for (index, value) in shoppingList.enumerate() {
print("Item \(index + 1): \(value)")
}
enumerate() provides a safe pattern for modifying array elements while iterating over them:
for (index, (var value)) in shoppingList.enumerate() {
if value == "something" {
shoppingList[index] = "something-else"
}
}

How to compare two arrays in Kotlin?

Given some arrays in Kotlin
let a = arrayOf("first", "second")
val b = arrayOf("first", "second")
val c = arrayOf("1st", "2nd")
Are there built-in functions to the Kotlin std-lib that tests two arrays for (value) equality for each element?
Thus resulting in:
a.equals(b) // true
a.equals(c) // false
equals() is actually returning false in both cases, but maybe there are built-in functions to Kotlin that one could use?
There is the static function java.utils.Arrays.deepEquals(a.toTypedArray(), b.toTypedArray()) but I would rather prefer an instance method as it would work better with optionals.
In Kotlin 1.1 you can use contentEquals and contentDeepEquals to compare two arrays for structural equality. e.g.:
a contentEquals b // true
b contentEquals c // false
In Kotlin 1.0 there are no "built-in functions to the Kotlin std-lib that tests two arrays for (value) equality for each element."
"Arrays are always compared using equals(), as all other objects" (Feedback Request: Limitations on Data Classes | Kotlin Blog).
So a.equals(b) will only return true if a and b reference the same array.
You can, however, create your own "optionals"-friendly methods using extension functions. e.g.:
fun Array<*>.equalsArray(other: Array<*>) = Arrays.equals(this, other)
fun Array<*>.deepEqualsArray(other: Array<*>) = Arrays.deepEquals(this, other)
P.S. The comments on Feedback Request: Limitations on Data Classes | Kotlin Blog are worth a read as well, specifically comment 39364.
Kotlin 1.1 introduced extensions for comparing arrays by content:
contentEquals and contentDeepEquals.
These extensions are infix, so you can use them the following way:
val areEqual = arr1 contentEquals arr2
And if you want to compare contents of two Collections ignoring the order you can add this simple extension:
infix fun <T> Collection<T>.sameContentWith(collection: Collection<T>?)
= collection?.let { this.size == it.size && this.containsAll(it) }
...and use it like this:
a = mutableListOf<String>()
b = mutableListOf<String>()
isListsHasSameContent = a sameContentWith b
For a simple equals (not deep equals!):
otherArray.size == array.size && otherArray.filter { !array.contains(it) }.isEmpty()
This code will compare the size and the items. The items are compared with .equals().
In koltin if your array or ArrayList is type of a data Class you can simply compare array :a and array :b
like this
if(a == b)
it will return simple boolean if it matched all the value of both arrays, but if you are matching other-than data data Class then you can use this extension to compare it with single value
fun <T> Array<T>.isEqual(comparable: Array<T>): Boolean {
var isChanged = true
if (this.size == comparable.size) {
for (index in 0 until comparable.size) {
if (this[index] != comparable[index]) {
isChanged = false
break
}
}
} else {
isChanged = false
}
return isChanged
}
then you can use your array
val a = arrayOf("first", "second")
val b = arrayOf("first", "second")
println(a.isEqual(b)) //true
a[0] = "third"
println(a.isEqual(b)) //false

Resources