This question already has answers here:
Sort Dictionary by keys
(16 answers)
Closed 5 years ago.
I have a dictionary with keys in format: [1:ABC, 113:NUX, 78:BUN, 34:POI, 223:NTY]
When I sorted the array of keys, i get the sorted key array as: [1:ABC, 113:NUX, 223:NTY, 34:POI, 78:BUN]
But I want the sorted array as: [1:ABC, 34:POI, 78:BUN, 113:NUX, 223:NTY]
what am I missing here? what additional sort should I add?
* I am using Swift 2
i got the answer:
let arrSortedKeys = keys.sort{$0.localizedStandardCompare($1) == NSComparisonResult.OrderedAscending}
Related
This question already has answers here:
How do I remove blank elements from an array?
(21 answers)
Closed 4 years ago.
How do I get rid of whitespaces and blank items in an array
names = ["alice", "", "bob", " ", "yankee"]
desired result should be
["alice", "bob", "yankee"]
In rails is simple, use present?
names.select(&:present?)
If you try this below it will provide you with a new array assigned to newNames that does not include the empty elements.
newNames = names.reject { |n| n.empty? }
This question already has answers here:
How to sort an array of custom objects by property value in Swift
(20 answers)
Closed 6 years ago.
Lets say I have the following array of Bus objects:
var buses = [Bus]()
After the buses array fills up I would like to sort the array of buses by bus number (which is a String for example "501"). Each Bus object has a bus number (buses[index].number). There are no duplicate bus numbers. How can I I do this sort? I see filter around but i'm not really sure how to apply it.
It's so simple by sort method in swift,
let sortedBuses = buses.sort({ $0.number > $1.number })
or
buses.sortInPlace({ $0.number > $1.number }) // this sorts arrays and saves it in self.
This question already has answers here:
How to perform sql "LIKE" operation on firebase?
(5 answers)
Closed 6 years ago.
Firebase queryEqualToValue returns items EQUAL to the specified key or value. How can we query for items PARTIALLY MATCH to the specified key or value?
Firebase iOS retrieve data documentation
We can combine startAt() and endAt() to limit both ends of our query.
The following example finds all dinosaurs whose name starts with the
letter "b":
let ref = Firebase(url:"https://dinosaur-facts.firebaseio.com/dinosaurs")
ref.queryOrderedByKey().queryStartingAtValue("b").queryEndingAtValue("b\u{f8ff}")
.observeEventType(.ChildAdded, withBlock: { snapshot in
println(snapshot.key)
})
The f8ff character used in the query above is a very high code point
in the Unicode range. Because it is after most regular characters in
Unicode, the query matches all values that start with a b.
Retrieving Data
This question already has answers here:
How can I sort an NSMutableArray alphabetically?
(7 answers)
Closed 8 years ago.
I have an NSMutableArray with this values:
how, format, anderson, babilon,
I would like to know if there is a command in which you can arrange this array in alphabetical order, so that the end result of the array becomes this:
anderson, babilon, format, how
observation -> my example above 4 items are specified in an array, but actually this array can store more than 1000 items, Someone can help me?
I would like to know if there is a command in which you can arrange
this array in alphabetical order, so that the end result of the array
becomes...
Yes. If you look at the NSMutableArray documentation you'll find a list of methods that can be used for sorting. They all start with the word "sort".
Apple also provides documentation on sorting arrays in the section named Sorting Arrays in Collection Programming Topics. For example, you could use the method -sortUsingSelector: this way:
NSMutableArray *names = [#[#"how", #"anderson", #"format", #"babilon"] mutableCopy];
[names sortUsingSelector:#selector(compare:)];
This question already has answers here:
Ruby ampersand colon shortcut [duplicate]
(2 answers)
What does map(&:name) mean in Ruby?
(17 answers)
Closed 9 years ago.
Here's the line of code I'm trying to wrap my head around:
Category.all.map(&:id).each { |id| Category.reset_counters(id, :products) }
Hoping someone can help me understand what (&:id) is doing and how it impacts the rest of the line? I believe it turns the symbol :id into a proc that'll respond to id?!? But then it gets confusing...
Thanks in advance!
Category.all.map(&:id)
is shorthand for
Category.all.map { |a| a.id }
as for how it affects the rest of the line, the above section returns all id values as a single Array. This Array of ids is then passed into another call to each, which iteratively passes each id into reset_counters.