Answer the question
In order to leave comments, you need to log in
Array.sort(): front-end != back-end?
Good afternoon.
Quite unexpectedly, I ran into a problem - sorting an array in a browser and a node.js server works differently. More precisely, everything is fine in the browser, but there is a problem on the backend.
Initial data:
var a = [{ distributor: 'igx4u_com', price: '24.99' },
{ distributor: 'online-gold*de', price: '18.35' },
{ distributor: 'gametrack', price: '35.95' },
{ distributor: 'de.gamecard.mobi', price: '16.75' },
{ distributor: 'playsmarty2008', price: '35.95' },
{ distributor: 'rpg-services', price: '26.9' },
{ distributor: 'keymbo*com', price: '16.85' },
{ distributor: 'de.gamecard.mobi', price: '24.99' },
{ distributor: 'online-gold*de', price: '16.85' },
{ distributor: 'keys4games_shop', price: '79.99' },
{ distributor: 'de.gamecard.mobi', price: '16.99' },
{ distributor: 'igx4u_com', price: '16.99' },
{ distributor: 'keymbo*com', price: '11.9' },
{ distributor: 'online-gold*de', price: '14.95' },
{ distributor: 'playsmarty2008', price: '26.85' },
{ distributor: 'rpg-services', price: '19.9' },
{ distributor: 'timegold_europe', price: '19.44' },
{ distributor: 'keymbo*com', price: '16.85' },
{ distributor: 'rpg-services', price: '18.9' },
{ distributor: 'www_g2a_com', price: '16.85' }
];
function f(arr, field1, field2) {
var mysort = function (field1, field2) {
return function (x, y) {
return (x[field1] == y[field1]) ? x[field2] > y[field2] : x[field1] > y[field1];
}
};
return arr.sort(mysort(field1, field2));
}
Object { distributor="de.gamecard.mobi", price="16.75"}
Object { distributor="de.gamecard.mobi", price="16.99"}
Object { distributor="de.gamecard.mobi", price="24.99"}
Object { distributor="gametrack", price="35.95"}
Object { distributor="igx4u_com", price="16.99"}
Object { distributor="igx4u_com", price="24.99"}
Object { distributor="keymbo*com", price="11.9"}
Object { distributor="keymbo*com", price="16.85"}
Object { distributor="keymbo*com", price="16.85"}
Object { distributor="keys4games_shop", price="79.99"}
Object { distributor="online-gold*de", price="14.95"}
Object { distributor="online-gold*de", price="16.85"}
Object { distributor="online-gold*de", price="18.35"}
Object { distributor="playsmarty2008", price="26.85"}
Object { distributor="playsmarty2008", price="35.95"}
Object { distributor="rpg-services", price="18.9"}
Object { distributor="rpg-services", price="19.9"}
Object { distributor="rpg-services", price="26.9"}
Object { distributor="timegold_europe", price="19.44"}
Object { distributor="www_g2a_com", price="16.85"}
{ distributor: 'de.gamecard.mobi', price: '16.99' }
{ distributor: 'igx4u_com', price: '24.99' }
{ distributor: 'gametrack', price: '35.95' }
{ distributor: 'de.gamecard.mobi', price: '16.75' }
{ distributor: 'igx4u_com', price: '16.99' }
{ distributor: 'de.gamecard.mobi', price: '24.99' }
{ distributor: 'online-gold*de', price: '14.95' }
{ distributor: 'keymbo*com', price: '16.85' }
{ distributor: 'keys4games_shop', price: '79.99' }
{ distributor: 'online-gold*de', price: '16.85' }
{ distributor: 'playsmarty2008', price: '26.85' }
{ distributor: 'rpg-services', price: '18.9' }
{ distributor: 'timegold_europe', price: '19.44' }
{ distributor: 'www_g2a_com', price: '16.85' }
Answer the question
In order to leave comments, you need to log in
It looks like you are not returning the sort result correctly. I assume that the browser swallows it, but the node does not.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question