Estoy trabajando en express Api con mongoose para crear get Api para mi proyecto. Pude hacer una llamada con éxito. Pero no estoy seguro de cómo hacer una API para ordenar datos por diferentes campos 
Modelo
id, productName, costPrice, soldPrice router.get("/sellProduct", (req, res, next) => { // condition if(req.query.product){ Product.find({prodName:req.query.product} ).then(data => { if (data) { res.status(200).send(data) } }) } // WHAT SHOULD BE THE SORT LOGIC TO SORT BY DIFF FIELD else if(req.query.sortBy){ Product.find({}).sort().then(data => { if (data) { res.status(200).send(data) } }) } else{ Product.find().then(data => { if (data) { res.status(200).send(data) } }) } });Soy beigneer y estoy haciendo mi mejor esfuerzo, pero cualquier ayuda será apreciada.
Puede crear los parámetros para .find y .sort dinámicamente:
router.get("/sellProduct", (req, res, next) => { const findParams = {}; const sortParams = { lowerCostPrice: { costPrice: 1 }, higherCostPrice: { costPrice: -1 }, lowerSoldPrice: { soldPrice: 1 }, higherSoldPrice: { soldPrice: -1 }, /* add more sort options ... */ }[req.query.sortBy]; if (req.query.product) findParams.prodName = req.query.product /* add more search options ... */ Product.find(findParams).sort(sortParams).then(data => { if (data) { res.status(200).send(data); } else { res.status(404); } }).catch(err => { console.log(err); res.status(500); }); });Si entiendo su pregunta correctamente, puede agregar un bloque de cambio y, según el valor pasado, ordenar los productos:
router.get('/sellProduct', (req, res, next) => { let result; // ... if (req.query.sortBy) { switch (req.query.sortBy) { case 'lowerCostPrice': { result = await Product.find({}).sort({ price: 'asc' }); break; } case 'higherCostPrice': { result = await Product.find({}).sort({ price: 'desc' }); break; } // and so on... } } // ... res.status(200).send(result); });