I have an array like this:
array = ["git-hw-abcd", "svn-hw", "svn-hw-design","git-hw"]
If I do array.sort I would basically get an ascending sort like this
["git-hw", "git-hw-abcd", "svn-hw", "svn-hw-design"]
Would like to sort the array by the values "svn-hw" and "git-hw" to appear as the first and second elements of the array so that I would get:
[ "svn-hw","git-hw",... then the rest of the values]
Any help would be appreciated.
Try this
* sorting on length(as sort criteria is not specified)
["git-hw-abcd", "svn-hw", "svn-hw-design","git-hw"].sort{|a,b| a.length <=> b.length}
It returns
["git-hw", "svn-hw", "git-hw-abcd", "svn-hw-design"]
array.sort{|a,b| a.split("-h")[1]<=>b.split("-h")[1]}
this is what you want
#Hivltg
try the length order using this :array= [ "svn-hw", "git-hw-abcd","git-hw", "svn-hw-design", "git-hw-bassics"]
you will find out the problem.
Not exactly sure how you're ordering, but here's an example of using a block to sort
array.sort{|a,b| a[4..-1] <=> b[4..-1]}
Related
Persoane = []
Nume = gets
Persoane.push Nume.split(",")
puts Persoane.sort
I am trying to get an user to input carachters that get split into substrings which get inserted in an array, then the program would output the strings in alphabetical order. It doesnt seem to work and I just get the array's contents, like so:
PS C:\Users\Lenovo\Desktop\Ruby> ruby "c:\Users\Lenovo\Desktop\Ruby\ruby-test.rb"
Scrie numele la persoane
Andrei,Codrin,Bradea
Andrei
Codrin
Bradea
PS C:\Users\Lenovo\Desktop\Ruby>
you can do this :
Nume = gets
puts Nume.split(",").sort
or in 1 line
array = gets.chomp.split(",").sort
The error is because of your use of push. Let's assume that you define the constant Nume by
Nume='Andrei,Codrin,Bradea'
Then, Nume.split(',') would return the Array ['Andrei', 'Codrin', 'Bradea']. When you do a Persoane.push, the whole array is added to your array Persoane as a single element. Therefore, Persoane contains only one Element, as you can verify when you do a
p Persoane
If you sort a one-element array, the result will also be just that one element - there is nothing to sort.
What you can do is using concat instead of push. This would result in Persoane being a 3-element array which can be sorted.
I'm not sure you need use constants here
If you don't need keep user input and use it somewhere, you can just chain methods like this
persons = gets.chomp.split(",").sort
For something a little different, let's not split at all.
people = gets.scan(/[^,]+/).map(&:strip).sort
This will avoid problems like multiple commas in a row yielding empty strings. Of course, you could also avoid that with:
people = gets.split(/\,+/).map(&:strip).sort
The JSONata doc "top-level-arrays-nested-arrays-and-array-flattening" covers the "flatten" case of an array of objects, each of which contains a property that contains an array value.
However, I have not been able to figure out how to flatten an array of arrays.
Q: What is the JSONata query to flatten an array of arrays?
input
[ [1,2], [], [3] ]
desired
[ 1, 2, 3 ]
I have figured out that flattening an array of arrays can be accomplished by using the $reduce function to iteratively apply the $append function.
$reduce($, $append)
for this simple test case:
$reduce( [ [1,2], [], [3] ], $append)
Q: Are there other ways to flatten an array of arrays in JSONata?
In JSONata, iterating over all the elements of an array returns a flattened array of the elements appended together... So it's really as simple as:
$.*
Almost looks like an emoji! ;*)
Technically, you don't even need the $. prefix -- but just using the expression * doesn't look right to me...
I've got an array of objects which I pull from the database. But I can sort them only in ascending or descending order from database, however I need them in custom order.
Let's say I have an array of objects from db :
arr = [obj1,obj2,obj3]
where obj1 has id 1, obj2 has id 2 and obj3 has id 3
but my sort order would be 3,1,2 or I'd have some array of ids which would dictate the order i.e [3,1,2]
So the order of custom sorting would be :
arr = [obj3,obj1,obj2]
I've tried :
arr.sort_by{|a,b| [3,1,2]}
I've been reading some tutorials and links about sorting and it's mostly simple sorting. So how would one achieve the custom sorting described above?
You're close. [3,1,2] specifies an ordering, but it doesn't tell the block how to relate it to your objects. You want something like:
arr.sort_by {|obj| [3,1,2].index(obj.id) }
So the comparison will order your objects sequentially by the position of their id in the array.
Or, to use the more explicit sort (which you seem to have sort_by slightly confused with):
arr.sort do |a,b|
ordering = [3,1,2]
ordering.index(a.id) <=> ordering.index(b.id)
end
This is like #Chuck's answer, but with O(n log n) performance.
# the fixed ordering
ordering = [3, 1, 2]
# a map from the object to its position in the ordering
ordering_index = Hash[ordering.map(&:id).each_with_index.to_a]
# a fast version of the block
arr.sort_by{|obj| ordering_index[obj.id]}
I have a method which returns the number of hotels from a webpage:
hotel_count = self.getHotelsList.values
The output of this method is:
[["hotel_0", "hotel_1", "hotel_2", "hotel_3", "hotel_4", "hotel_5", "hotel_6", "hotel_7", "hotel_8", "hotel_9", "hotel_10", "hotel_11", "hotel_12", "hotel_13", "hotel_14", "hotel_15", "hotel_16", "hotel_17", "hotel_18", "hotel_19", "hotel_20", "hotel_21", "hotel_22", "hotel_23", "hotel_24", "hotel_25", "hotel_26", "hotel_27", "hotel_28", "hotel_29", "hotel_30", "hotel_31", "hotel_32", "hotel_33", "hotel_34", "hotel_35", "hotel_36", "hotel_37", "hotel_38", "hotel_39", "hotel_40"]]
I want to know the length of this array, but if I write
hotel_count = self.getHotelsList.values.length
The length is 1. How can I get a length of 41, which is the one I'm expecting?
Thanks
The array you are showing is nested inside another array. So the outer array is of length 1, the inner array is what you want.
To get it you have to first get the first element of the outer array using [0] or first
testList[0].length
testList.first.length
I am not sure why your getHotelsList method returns a nested array, it doesn't appear to need it.
hotel_count = getHotelsList.values.first.length
You can also do it with [0], but first is faster.
Two notes:
You don't need self at the beginning.
It is a bad habit to use camel case for method names in Ruby. it should better be get_hotels_list.
You could convert that into a single array with flatten:
hotel_count = self.getHotelsList.values.flatten.size
How do i reverse the element of this array using lambda ..
array = ["Hello","World"]
How do i reverse the elements in the array so that it is like this:
array = ["olleH","dlroW"]
array.map(&:reverse)
should do the trick
I don't see why would you use lambda for this but who am I to judge :)
lambda{|a| a.map(&:reverse)}.call(["Hello","World"].reverse)