Posts

Showing posts with the label sorting

Why wont my calculator function complete the math?

Why wont my calculator function complete the math? Hi so I'm having trouble figuring out why my function will do the division but leave the multiplication as an array without completing the math. Here's the code: const mathObj = { "*": function(a , b) {return a * b}, "/": function(a , b) {return a / b}, "+": function(a , b) {return a + b}, "-": function(a , b) {return a - b} } const arr = [ 10, "/" , 2 , "*" , 10 , "/" , 2 ]; function solveTheMath(arr) { const len = arr.length; for(let i = 0 ; i < len ; i++){ if(arr[i] === "/" || arr[i] === "*"){ const sliced = arr.slice(i - 1 , i + 2); var mathResult = mathObj[arr[i]](sliced[0], sliced[2]); arr.splice(i - 1 , 3, mathResult); console.log(arr); //[5*5] } } } solveTheMath(arr); Why does...

SQL Query to show instock always but do 2 lots of ordering

SQL Query to show instock always but do 2 lots of ordering Not sure how this would work but I can give a brief description. I have database I want to sort but kind of sort twice in same statement. I have products, some in stock and some out of stock, I want to always show in stock before any out of stock regardless of sorting order. So it will sort A-Z of all in stock items first and the same query has to then show A-Z of out of stock. Sorting in price order would show Low-High of in stock and then do the same for out of stock again in same query. Basically, I want to be sure they can always see all the in stock items first at all times. 1 Answer 1 You can use multiple terms in the order by clause: order by SELECT * FROM products ORDER BY in_stock DESC, name ASC By clicking "Post Your Answer", you acknowledge that you have read our updated...

Sort by 2 variables prioritizng 1

Sort by 2 variables prioritizng 1 I have a list of posts . These posts have a date ( created ) and a score ( score ). What I would like to do is sort them by created and score but having score as the more important variable.. posts created score created score score In other words sort all posts in order of created but having the highest score show first.. created score So essentially: score: 6, created: july 1 //created is actually a timestamp 1530372915676 score: 4, created: july 1 score: 2, created: july 1 score: 1, created: july 1 score: 5, created: june 30 score: 3, created: june 30 I found this piece of code: array.sort(function (x, y) { return x.created - y.created || x.score - y.score; }); but it doesn't seem to work.. What would the best way of doing this be? There seems to be a contradiction in your question. You say that score should be "the most important variable", but your example shows that the most important variable is cre...

Sort / filter multiple objects in JQ by date

Sort / filter multiple objects in JQ by date I'm trying to use JQ to find the most recent artifact in a Nexus API query. Right now, my JSON output looks something like: { "items" : [ { "downloadUrl" : "https://nexus.ama.org/repository/Snapshots/org/sso/browser-manager/1.0-SNAPSHOT/browser-manager-1.0-20180703.144121-1.jar", "path" : "org/sso/browser-manager/1.0-SNAPSHOT/browser-manager-1.0-20180703.144121-1.jar", "id" : "V0FEQS1TbmFwc2hvdHM6MzhjZDQ3NTQwMTBkNGJhOTY1N2JiOTEyMTM1ZGRjZWQ", "repository" : "Snapshots", "format" : "maven2", "checksum" : { "sha1" : "7ac324905fb1ff15ef6020f256fcb5c9f54113ca", "md5" : "bb25c483a183001dfdc58c07a71a98ed" } }, { "downloadUrl" : "https://nexus.ama.org/repository/Snapshots/org/sso/browser-manager/1.0-SNAPSHOT/browser-manager-1...