K
K
Keenest2013-06-27 17:54:19
JavaScript
Keenest, 2013-06-27 17:54:19

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 code:
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));
}

That is, the function sorts an array of objects by two fields (in ascending order). Correct result:
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"}

node.js result:
{ 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' }

PS naming functions, variables, data, etc. - in this case, it doesn't matter, because this is a highly simplified example.
What's the matter?
Is it really necessary to write something of your own instead of the default .sort()?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
creage, 2013-06-27
@creage

It looks like you are not returning the sort result correctly. I assume that the browser swallows it, but the node does not.

A
Antelle, 2013-06-27
@Antelle

And on what version of the node is this repeated? Something I don't remember sort removing duplicates somewhere.
This doesn't happen to me. Can you post the script you're running somewhere?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question