This question already has answers here:
How to sort an array of hashes in ruby
(5 answers)
Closed 6 years ago.
I have a array of hash something like
[{a1: 'a', b1: 'b'},{a1: 'c', b1: 'b'},{a1: 's', b1: 'cq'}]
I want to sort the array in the ascending value of b1 and return an array. Sorry if its easy but got confuse with the logic.
Following will return you an array sorted based on b1
array_of_hashes.sort_by { |hsh| hsh[:b1] }
Related
This question already has answers here:
How to sort based on/compare multiple values in Kotlin?
(3 answers)
Closed 2 years ago.
Let's say I have the following code:
val list = mutableListOf("abab", "abcd", "aaa")
list.sortBy { it.length } //result: [aaa, abab, abcd]
This sorts the list by the lengths of the Strings.
How do I break draws (2 Strings of the same length) by some other rule, lets say number of appearance of the char 'a'.
This way, one would have a hirarchy of comparison-rules: First length, then break draws by number of 'a', then maybe some other rule.
The function sortBy only receives a selector, which maps the elements to a comparable value, which is not capable for what I want to do I think.
Use sortWith and a custom comparator
val list = mutableListOf("abab", "abcd", "aaa")
list.sortWith(compareBy(String::length).thenBy { it.count { char -> char == 'a'} })
Here you can see documentation for all functions that will help you create a new comparator:
kotlin.comparisons
This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
f = open("dict.txt","a")
f.write("Customer_Name: "+Name+"\nMovies: "+Movies_Name+"\nQuantity: "+str(quantity)+"\n")
f.close()
Movies_Name is a list which might contain more than one movies. Now I want to write the names of the movies individually instead of writing them as a list.
For me it has nothing to do with discord itself. It is just Python question.
So answer can be found here
By using ''.join
list1 = ['1', '2', '3']
str1 = ''.join(list1)
In your case:
f.write("Customer_Name: "+Name+"\nMovies: "+''.join(Movies_Name)+"\nQuantity: "+str(quantity)+"\n")
This question already has answers here:
How to declare an empty 2-dimensional array in Ruby?
(5 answers)
Closed 6 years ago.
I have create empty array of array like below , Is there any other best way to initialize it ?.
arr = [[],[],[],[],[],[],[],[],[]]
I think the best way to achieve it by using Array class.
ex:
Array.new(width){Array.new(height)}
you can also provide width & height value like width = 2 & height = 4
Have you tried something like
arr = Array.new(9) { Array.new }
?
This question already has answers here:
How to use d3.min and d3.max within a d3.json command
(3 answers)
Closed 8 years ago.
If I want the maximum value of an array, I can do this:
var data = [1,2,3,4,5];
console.log(d3.max(data)); //output 5
But if I try with an associative array, I get 'undefined':
var data = {'foo' : 4, 'baz' : 8};
console.log(d3.max(data)); //output undefined
How do I use d3.max() with an associative array?
Based on help from the comments to the question, this was the solution:
d3.max(d3.values(data));
This question already has answers here:
How can I find out the position of an item contained by an array?
(3 answers)
Closed 9 years ago.
Let's say I have the following array:
array = ["a","a","a","a","a","a","b","b","b","b","b","b"]
I want to find the index of the first "b" in the array. What is the best way of doing it?
Use Array#index
for first occurrence and Array#rindex for last occurrence of an element.
array = ["a","a","a","a","a","a","b","b","b","b","b","b"]
array.index("b") # => 6
array.rindex("b") # => 11