Sorting collection of maps based on values [closed] - java-stream

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm trying to sort list of maps based on specific key lets say "tax" with int values in all the maps. However other keys like "id" and "price" have string and double type values respectively as shown below.
List<Map<String, Object>> taxes = Arrays.asList(Map.of("id", "3", "tax", 25, "price", 8.05),
Map.of("id", "9", "tax", 37, "price", 16.05), Map.of("id", "2", "tax", 19, "price", 9.25),
Map.of("id", "11", "tax", 28, "price", 16.05));
I want to sort list of maps with highest tax value to lowest tax value. I'm trying with below approach but getting error at m.get("tax").
list.stream()
.sorted(Comparator.comparing(map -> map.get("tax")).reversed())
.collect(Collectors.toList());`

You are getting the error because compile could not interpret the type of object in a lambda expression, you can solve it but just interpreting map as Map<String, Integer>
list.stream()
.sorted(Comparator.comparing((Map<String,Integer> map) -> map.get("tax")).reversed())
.collect(Collectors.toList());
But this is not completely fixed and it will fail when tax is not presenting in any Map or having null as value, you can solve that using Comparator.nullsFirst or Comparator.nullsLast
List<Map<String,Integer>> taxes = List.of(Map.of("id",3, "tax",25),
Map.of("id",9, "tax",37),Map.of("id",2, "tax",19),
Map.of("id",11));
List<Map<String,Integer>> results = taxes.stream()
.sorted(Comparator.comparing((Map<String,Integer> map)->map.get("tax"),Comparator.nullsFirst(Comparator.naturalOrder())).reversed())
.collect(Collectors.toList());
System.out.println(results); //[{tax=37, id=9}, {tax=25, id=3}, {tax=19, id=2}, {id=11}]

Related

Assign point values to Sheets checkboxes and find sum

I am using Google Forms to create a survey with weighted answers. I've been able to make things work when there is just one possible correct answer - I made a separate tab with a set of answer tables with point values assigned, then used vlookup to call back and match the given response to the answer table and fetch the assigned point value.
=VLOOKUP(P2, Sheet2!$A$49:$B$50, 2, FALSE)
P2 is a value pulled from the "Form Responses" tab - in this case, a yes/no answer. Sheet 2 has a table for each question with the possible answers and the point values for each answer (A49=yes, A50=no)
However, for some of the questions, multiple answers are valid and I want to add up the total number of points for that given question. So for example:
What are your hobbies? and folks can choose from
Riding your bike
Playing football
Swimming
Going fishing
Painting
And the respective point values are 2, 2, 3, 4, 4
So then, if someone chose the "Swimming" and "Going fishing" checkboxes in the form, I'd get "7", and if someone chose "Riding your bike", "Playing football", and "Painting", I'd get "8".
I realize that the output from the Google form will list the chosen answers all in one cell (Playing football, Going fishing), so I'm not sure how to make it count each answer (especially since some of them are multi-word answers) and output the sum of the values.
VLOOKUP is not suitable in this case. try FILTER like:
=FILTER(Sheet2!B49:B50, Sheet2!A49:A50=P2)
then VLOOKUP it like:
=SUMPRODUCT(IFNA(VLOOKUP(FILTER(Sheet2!B49:B50, Sheet2!A49:A50=P2), sheetx!A:B, 2, 0)))
where sheetx!A:B is like:
Riding your bike
2
Playing football
2
Swimming
3
Going fishing
4
Painting
4
and if Sheet2!B49:B50 contains multiple comma+space separated values you will need to split them like:
=SUMPRODUCT(IFNA(VLOOKUP(FILTER(
IFERROR(SPLIT(Sheet2!B49:B50, ", ")), Sheet2!A49:A50=P2), sheetx!A:B, 2, 0)))

Return cell from another sheet if it contains string in column [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
I am working on a school rooming timetable system. I am finding a formula which finds if a particular room is used during a specific period by which class and teacher.
There is a staff overview with clean data of which class and room they are in, alongside preferred results:
https://docs.google.com/spreadsheets/d/1H9Q8K9KBLgqCsvVZ_ekqsLPbZKeMzvldDnyI-KerOD4/edit?usp=sharing
For example, for on the overview, Monday Period 1 (Column B) Staff 1 uses room BG01 for the class 07MA01. Thus on the room overview, it shows that in Period 1, Staff 1 uses BG01 for 07MA01.
Use filter() and regexmatch(), like this:
=iferror(
textjoin( "; ", true,
filter(
regexreplace('Staff Overview'!B$3:B, "#.+", "") & "(" & trim('Staff Overview'!$A$3:$A) & ")",
regexmatch('Staff Overview'!B$3:B, $A3)
)
),
"FREE"
)

How to use gradient descent on data that has string values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to solve the predicting house pricing problem (https://www.kaggle.com/c/house-prices-advanced-regression-techniques/data)
How could I transform string data into numerical data in Octave?
The link is paywalled, but its title mentions the word 'categorical', so I'm assuming that by 'numerical' you mean integer labels, rather than parsing a string that represents a number to its equivalent float.
So with that in mind, here's a typical way to represent this.
Indices = [ 1,2,3,2,3,2,1,2,1,2,3,1,3,3,1 ];
Labels = { 'class1', 'class2', 'class3' };
It really is as simple as that. If you really want this to be a single 'variable', you can collect it into a struct:
MyCategoricalVariable = struct( 'indices', Indices, 'labels', Labels );
Obviously it depends how the data is provided to you in the first place. If you're given the strings instead of the labels, you can convert it to an indices/labels pair like so:
Data = { 'a', 'b', 'c', 'c', 'b', 'c', 'b', 'a', 'a', 'a', 'b' };
Labels = unique( Data );
[~, Indices] = ismember( Data, Labels )
There are two possibilities of string data
Just one or two words, and value of test won't change (i.e. for a column it would take fix value of text), then you can use Labels. that would solve your problem.
if you have column which can take any value and length is also not fixed, you can first do TF-IDF and based on it you can train your model.

Alphabetizing data from the filter function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have data from multiple tabs in Google Sheets that I am combining into one master tab. I would then like the data on the master tab to be alphabetized based on the last name in one column automatically if more information is added. Here is the formula I used to combine the multiple tabs onto the master tab:
={filter(tab1!A5:Z, tab1!B5:B<>"");FILTER(tab2!A5:Z, tab2!B5:B<>""); FILTER(tab3!A5:Z, tab3!B5:B<>""); FILTER(tab4!A5:Z, tab4!B5:B<>"");FILTER(tab5!A5:Z, tab5!B5:B<>""); FILTER(tab6!A5:Z, tab6!B5:B<>"")}
I have not been able to find anything that would work when I google "how to sort data in abc order from a filter function google sheets".
You can use SORT() and there's even an example in its documentation that applies to your situation:
SORT({1, 2; 3, 4; 5, 6}, 2, FALSE)
So if the last name is in column B, which is the second column you could write (formatted for clarity)
=SORT(
{
FILTER(tab1!A5:Z, tab1!B5:B<>"");
FILTER(tab2!A5:Z, tab2!B5:B<>"");
FILTER(tab3!A5:Z, tab3!B5:B<>"");
FILTER(tab4!A5:Z, tab4!B5:B<>"");
FILTER(tab5!A5:Z, tab5!B5:B<>"");
FILTER(tab6!A5:Z, tab6!B5:B<>"")
},
2,
TRUE
)

Firebase real-time database substring query [duplicate]

This question already has answers here:
How to query firebase keys with a substring
(2 answers)
Firebase query - Find item with child that contains string
(1 answer)
Firebase get all usernames & user Id starting with user entered character
(3 answers)
How to get all elements whose id contains a particular substring?
(1 answer)
Closed 5 months ago.
I am going to look up with keyword substring "vi" for key "full_name". How can I do that?
[{
"full_Name": "David Beckham",
"email": "davidbeckham#gmail.com"} ,
{
"full_Name": "Cristiano Ronaldo",
"email": "critinanoronaldo#gmail.com"
}]
Thank you.
You can do something like this to order and query based on a certain key
dbQuery.orderByKey().startAt(SUBSTRING).endAt(SUBSTRING+"\uf8ff");
For more info you can look at the official documentation
https://firebase.google.com/docs/database/rest/retrieve-data#range-queries
Query firebase key with a substring is possible
let node = await db.ref('yourPath').orderByChild('full_Name').startAt('!').endAt('SUBSTRING\uf8ff').once('value');

Resources