Basically I want to build a function which sorts objects in an array by one of the object's properties/member variables. I am preeeety sure that the comparator function is where the error is hidden, but I am not 100% sure.
The output I should get after the sort function is called is 1,2,3. I get 1,3,2 which means that it is unchanged
This is the entire js code (with some comments):
var arr = [];
//object definition and creation
var main = document.getElementById("main");
var task = {
name: "",
priority: 0
};
//first
var one = Object.create(task);
one.priority = 1;
//secondd
var two = Object.create(task)
two.priority = 3;
//last
var three = Object.create(task);
three.priority = 2;
//append
arr.push(one);
arr.push(two);
arr.push(three);
//sort function
function sortT() {
arr.sort(compareFN);
}
//comperator function
function compareFN() {
return task.priority < task.priority;
}
function print() {
for (var i = 0; i < arr.length; i++) {
console.log(arr[i].priority);
}
}
//execution of the program
print();
sortT();
print();
EDIT: The solution is the following - As stated, the comparator function really was the problem, the correct way to write it is the following:
function compareFN(taskA, taskB) {
return taskA.priority < taskB.priority;
}
There are multiple problems with your comparator:
It refers to the global task object instead of the objects being compared.
It compares the object to itself.
It is supposed to perform a three-way comparison.
Try:
var compareFN = function(a, b) {
return a.priority - b.priority;
}
The compare function needs two arguments: the first and the second element it should compare.
So your compareFN should look like this:
function compareFN(taskA, taskB) {
return taskA.priority - taskB.priority;
}
Edit: As NPE said, it is supposed to perform a three-way comparison, so a simple a < b is not so a great idea here.
You need to change the signature of you compare function to include the two tasks.
For ascending order (normally what you want) you need to do b < a, a < b will do descending order
//comperator function
function compareFN(a, b) {
return b.priority < a.priority;
}
The comparator function returns a negative value, zero or a positive value. Those three comprise what is called here the three-way comparison.
So:
'''
function cmp((a, b) => {
// ascending
return a - b
}
'''
For descending return b - a
Related
I have the following code
function slotCalc(a, b) {
var array = ["default"];
while (a < b) {
var obj = { test: "test" };
array.push(obj);
b++;
}
return array;
}
I can not understand why the array is not returned. If I remove the while loop, its returned as expected.
How can a while loop do this?
The error stems from b++; - so you (infinitely) increase b, but a does not change. So it's always b > a unless when you call this function with such parameters thata >= b.
Perhaps you wanted a++; instead; or, b--;
I have code that's meant to take an input string, find the name of an operation (add, subtract, multiply, or divide) and do the indicated operation on the numbers also included in the input string. So if my input looks like this
1 a 2 b 3 c 4 d subtract
I want my program to find the word "subtract" and then subtract 2 from 1, 3 from the result and then 4 from the result of that. So far, I have my operations defined as global variables at the top of my program like this:
var operations = {
add: function(a, b) {
"use strict";
return Number(a) + Number(b);
},
subtract: function(a, b) {
"use strict";
return a - b;
},
multiply: function(a, b) {
"use strict";
return a * b;
},
divide: function(a, b) {
"use strict";
return a / b;
}
};
The area I'm having problems with is the while loops I'm trying to make in order to recognize the first instance of one of those operation names occurring in my input string, it looks like this:
function doArithment() {
"use strict";
var i = 0;
clearResults();
sepNsLs();
var found = false;
var q = 0;
var keys = Object.keys(operations);
while (q < keys.length) {
if (arrayses.letteros.indexOf(keys) !== -1) {
found = true;
break;
} else if (arrayses.letteros.indexOf(keys) === -1) {
q += 1;
}
}
if (found) {
var result = arrayses.numeros[0];
while (i < arrayses.numeros.length) {
if (i === 0) {
result = arrayses.numeros[0];
} else {
result = keys[q](result, arrayses.numeros[i]);
}
i += 1;
}
clearResults and sepNsLs should not have any bearing on whether or not this loop works properly, but if it's asked for, I can provide the code for both of those functions. arrayses.letteros is the array of non-numbers from my input string, and that's where I try to look for the operation names, arrayses.numeros is the array in which I put the numbers from my input string. I had a for-in statement working completely in this function, but JSLint doesn't like that, so I've been trying to make this work in other ways. Is there something I'm missing here?
You are searching for the entire keys array. You need to search for the individual element. Anywhere you are referencing keys you mean to be referencing keys[q].
arrayses.letteros.indexOf(keys[q])
This simple javascript
var x = new Array();
x[0] = 2.73;
x[1] = 11.17;
x[2] = 3.12
x.sort();
for(var i in x)
alert(x[i]);
produces the results:
11.17, 2.73, 3.12 instead of 2.73, 3.12, 11.17.
Why is that and how can I fix it?
Thanks in advance!
It's sorting alphabetically, try passing your own sorting function:
var x = new Array();
x[0] = 2.73;
x[1] = 11.17;
x[2] = 3.12;
numberSort = function (a,b) {
return a - b;
};
x.sort(numberSort);
for(var i in x) {
alert(x[i]);
}
By default, Array.sort will sort alphabetically (lexographically)...but you can supply your own function. Try:
x.sort(function(a, b) { return a > b ? 1 : -1});
Array.sort() function treats its elements as Strings and if no function is passed to the sort() statement, it converts the elements to Unicode and sorts. Therefore, it is advised to pass a custom sort Function whenever sorting numbers.
function customSort(a, b){
return a - b;
}
console.log([-11,-2, 0 ,100].sort(customSort));
This customSort() function will sort the array in_place in ascending order.
Between them, the existing answers tell you everything, but none of them mention both of the problems in your code. Here's the full answer:
The sort isn't doing what you want because the default sort is lexical (i.e. the array elements are converted to strings and compared alphabetically). You can provide your own comparison function to sort():
x.sort(function(a, b) {
return a - b;
});
Secondly, for...in is actually telling you nothing concrete about whether your array is sorted correctly, because the enumeration of for...in is not defined (even though most but not all browsers do broadly what you'd expect). Use a for loop instead (as indeed you generally should for arrays):
for (var i = 0, len = x.length; i < len; ++i) {
alert(x[i]);
}
You are not iterating properly. It should be:
for (var i = 0; i < x.length; i++) {
alert(x[i]);
}
When you use for..in in javascript this will loop through the properties of the object and the order of iteration is undefined. You should be seeing some strange output as well such as all the functions defined in the Array class.
I'm try to order objects numerically in an array in ascending order, by using a nested object property, the property being 'score'.
The Array;
[ {name:'dan', score:220},
{name:'lucy', score:876},
{name:'mike', score:211} ]
I found the following thread, but have yet managed to get it working.
How to sort a JavaScript array of objects by nested object property?
Console outputs undefined.
function order_top_scores(prop, arr) {
arr.sort(function (a, b) {
if (a[prop] < b[prop]) {
return -1;
} else if (a[prop] > b[prop]) {
return 1;
} else {
return 0;
}
});
};
function get_results(){
$.get(wp_theme_dir+'/dottodot.php',
function(data){
var returnedData = $.parseJSON(data);
var ordered_scores = order_top_scores(returnedData)
console.log(returnedData);
});
}
My Array differs slightly, could it be the second property thats disrupting the sort?
Or maybe the way i'm handling the data from the ajax request.
thanks in advance,
Cam
how are you?
I've just tested this, and there's a couple of things you might want to modify.
First of all, you're using the "sort" solution from the person who asked, not the actual solution, so you first need to rewrite your order_top_scores, like this:
var order_top_scores = function (prop, arr,reverse) {
if(!reverse) reverse = 0;
prop = prop.split('.');
var len = prop.length;
arr.sort(function (a, b) {
var i = 0;
while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
if(reverse){arr.reverse()};
return arr;
};
Analyzing this function, I've added a third "reverse" parameter, which expects either true or false (because the original solution orders it from lowest to highest, and in this case you want the opposite)
Now that you have this function, there's 2 things you want to keep in mind:
First
In this line:
var ordered_scores = order_top_scores(returnedData);
You are not sending the first mandatory parameter, which actually tells the function which property you want the object sorted by: In this case, "score".
So, you have to call the function like this:
var ordered_scores = order_top_scores('score',returnedData);
And if you want it to be sorted from high to low, like this:
var ordered_scores = order_top_scores('score',returnedData,true);
Second
Also, keep in mind you are outputting the "returnedData" value rather than the ordered_scores value so if this line:
console.log(returnedData);
Is outputting undefined, it means your JSON data is not correct. To be sure the sort worked, you should also output the ordered_scores like this:
console.log(ordered_scores);
Let me know if anything unclear.
Cheers!
I am not sure this is the correct code:
var returnedData = $.parseJSON(data);
var ordered_scores = order_top_scores(returnedData)
The following change in order_top_scores and method call may work correctly if returnedData is array as you have mentioned in your question:
function get_results(){
$.get(wp_theme_dir+'/dottodot.php',
function(data){
var returnedData = $.parseJSON(data);
var ordered_scores = order_top_scores("score", returnedData);
console.log(returnedData);
});
}
function order_top_scores(prop, arr) {
arr.sort(function (a, b) {
if (a[prop] < b[prop]) {
return -1;
} else if (a[prop] > b[prop]) {
return 1;
} else {
return 0;
}
});
}
You can check the output here in console
I have a function that computes product of numbers in an array. The function should work like this
function prod (array){
//compute and return product
}
var arr = [1,2,3,0,4,5,0,6,7,8,0,9];
the function call:
prod(arr); //should return 6
prod(arr); //should return 20
prod(arr); //should return 336 (6*7*8)
prod(arr); //should return 9
prod(arr); //should return 0
prod(arr); //should return 0
prod(arr); //should return 0
In scheme, this is done with continuations, by storing previous state of the function (state of the function is captured just before its exit point) see this
So, in short, I want the javascript function return different values at different times with same parameter passed everytime.
JavaScript is a well designed language, so I hope there must be something which can emulate this. If there happens to be nothing in JS to do it, I do not mind to conclude with failure and move on. So, feel free to say its impossible.
Thanks.
JavaScript is not capable of supporting continuations: it lacks tail-calls.
Generally I would write this to use a "queue" of sorts, although CPS is also do-able (just have a finite stack :-) Note that other state can also be captured in the closure, making it an "explicit continuation" of sorts ... in a very gross sense.
Example using a closure and a queue:
function prodFactory (array){
// dupe array first if needed, is mutated below.
// function parameters are always locally scoped.
array.unshift(undefined) // so array.shift can be at start
// also, perhaps more closured state
var otherState
// just return the real function, yippee!
return function prod () {
array.shift()
// do stuff ... e.g. loop array.shift() and multiply
// set otherState ... eat an apple or a cookie
return stuff
}
}
var prod = prodFactory([1,2,3,0,4,5,0,6,7,8,0,9])
// array at "do stuff", at least until "do stuff" does more stuff
prod() // [1,2,3,0,4,5,0,6,7,8,0,9]
prod() // [2,3,0,4,5,0,6,7,8,0,9]
prod() // [3,0,4,5,0,6,7,8,0,9]
Happy coding.
"Finished implementation". Although this particular problem can avoid array mutation and just use an index: the same concepts apply. (Well, slightly different. With just an index the closed over variable would be altered, whereas with this approach an object is mutated.)
function prodFactory (array) {
array = array.slice(0)
return function prod () {
var p = 1
for (var n = array.shift(); n; n = array.shift()) {
p *= n
}
return p
}
}
var prod = prodFactory([1,2,3,0,4,5,0,6,7,8,0,9])
prod() // 6
prod() // 20
prod() // 336
You can give the function a property that will be remembered between calls:
function prod (array){
if (typeof prod.index === "undefined" || prod.currentArray != array) {
prod.currentArray = array;
prod.index = 0;
}
if (prod.index >= array.length)
return 0;
//compute and return product
var p = 1,
c;
while (prod.index < array.length) {
c = array[prod.index++];
if (c === 0)
return p;
p *= c;
}
return p;
}
I'm just guessing from your description of what should be returned that on an individual call to the function it should take the product of all of the numbers up to but not including the next zero or the end of the array. Calls after the end of the array should return 0? I may have the algorithm wrong for that, but you get the idea for what I'm suggesting to remember the function state between calls.
I've added a property to remember the current array being processed. As long as you keep passing the same array in to the function it will continue with the next elements, but if you pass a different array it will reset...
you can try something like
var index = 0;
function prod (array){
if(index < array.length){
var prod=1;
for(int i=index;i<array.length;i++){
if(array[i] != 0){
prod = prod * array[i];
}
else{
index = i+1;
return prod;
}
}
}
return 0;
}
this will update the global variable index everytime the function is called.
What you're looking for here are generators. As of 1.7, JavaScript supports them.