Playground - Ordena los productos
Clase 49 de 99 • 30 días de JavaScript
Contenido del curso
Clase 49 de 99 • 30 días de JavaScript
Contenido del curso
Ruben Gallegos Hernandez
IRIDIAN GUADALUPE CARRERA MONDRAGÓN
Gustavo Alonso Aguilar Acuña
Alberto Vargas
Benjamin Rodriguez
Abril Darynka Tapia Sosa
Lucas Frazzetta
Randy Ochoa
David Ochoa
Alberto Vargas
Manufactura Bancor
Angel Ramírez
Andre Huaman Yovera
Andrés Soret Chacin
Juan Pablo Tavarez
Harrison Steve Pinzón Neira
Manuel Juarez
Milly Mileth Benítez Martínez
Ronaldo Jiménez
Salvador Gonzalez Blanco
Pablo Perez
Pariksit Erikrsnan Ortiz Flores
Carina Payleman
Alexis Corrales
Mi solución
export function sortByAvailabilityAndPrice(products) { return [...products].sort((producta, productb) => (productb.inStock - producta.inStock || producta.price - productb.price)); }
Una duda, qué está haciendo esta linea?
productb.inStock - producta.inStock
Mi respuestas: . . . . . . . . . .
let sorted = [...products] sorted.sort(function(a,b){ if(b.inStock > a.inStock) return 1; if(b.inStock < a.inStock) return -1; if(a.price > b.price) return 1; if(a.price < b.price) return -1; })
Me agrada que todo se resuleve en un solo sort
🛡️Escudo anti-spoilers🛡️
export function sortByAvailabilityAndPrice(products) { return [...products].sort((a, b) => { if (b.inStock != a.inStock) return a.inStock ? -1 : 1 return (a.price - b.price) }) }
Hola, dejo mi solucion Caminito anti spoilers 🛴✨ . . . . . . . . . . . . . . . . . . . . Llegamos 📍
const list = [...products]; list.sort((a, b) => a.price - b.price); list.sort((a, b) => b.inStock - a.inStock); return list
Super simple tu manera. Genial
Aqui esta mi solucion:
export function sortByAvailabilityAndPrice(products) { const nuevoArray = [...products] nuevoArray.sort((a, b) => a.price - b.price) nuevoArray.sort((a, b) => b.inStock - a.inStock) return nuevoArray }
Spoiler Alert
Resuelto en 4 lineas
Acepto sugerencias :)
Gatito anti-spoilers
export function sortByAvailabilityAndPrice(products) { // Tu código aquí 👈 const productsSorted = [...products]; productsSorted.sort((a, b) => a.price - b.price) productsSorted.sort((a, b) => b.inStock - a.inStock) return productsSorted; }
. . . . . . . . . . Intentaba algo similar
export function sortByAvailabilityAndPrice(products) { // Tu código aquí 👈 const productsClone = products.map((x) => x); productsClone.sort( (a, b) => { return a.price - b.price; } ); productsClone.sort((a, b) => { if (a.inStock && b.inStock) return 0; if (a.inStock) return -1; if (b.inStock) return 1; }); return productsClone; }
Mi solucion:
return [...products].sort((m, M) => m.price - M.price).sort((a, b) => b.inStock - a.inStock); }
Mi solución
function sortByAvailabilityAndPrice(products) { const orderProducts = [...products] const inStock = []; const outStock = []; orderProducts.forEach(product => { if(product.inStock == true) { inStock.push(product); } else { outStock.push(product); } }) inStock.sort((a, b) => a.price - b.price); outStock.sort((a,b) => a.price - b.price); const productsByAvailability = [...inStock, ...outStock]; return productsByAvailability; }
export function sortByAvailabilityAndPrice(products) { const sortedProducts = [...products]; sortedProducts.sort((a, b) => { // Ordenar por disponibilidad en inventario (true primero) if (a.inStock && !b.inStock) { return -1; } else if (!a.inStock && b.inStock) { return 1; } // Ordenar por precio ascendente return a.price - b.price; }); return sortedProducts; }
My solution 👇
function sortByAvailabilityAndPrice(products) { const copyOfProducts = [...products]; copyOfProducts.sort((a, b) => { if (a.inStock === b.inStock) { return a.price - b.price; } else { return b.inStock - a.inStock; } }); return copyOfProducts; }
Mi solución
export function sortByAvailabilityAndPrice(products) { let new_products = [...products].sort((a, b) => a.price - b.price) let resultado = [...new_products].sort((a, b) => b.inStock - a.inStock) return resultado }
Solución al desafio:
Mi solución:
export function sortByAvailabilityAndPrice(products) { // Tu código aquí 👈 let inStock = products.filter(product => product.inStock === true); let outStock = products.filter(product => product.inStock === false); inStock.sort((a, b) => a.price - b.price); outStock.sort((a, b) => a.price - b.price); let sortedProducts = inStock.concat(outStock); return sortedProducts; }
Mi aporte
export function sortByAvailabilityAndPrice(products) { let new_products = [...products].sort((a, b) => a.price - b.price) return new_products.sort((a, b) => b.inStock - a.inStock); }
Spoiler .. .. .. .. .. ..
function sortByAvailabilityAndPrice(products) { // clasificar podructos let productosDisponibles = products.filter(function(producto){ return producto.inStock == true }); let productosNoDisponibles = products.filter(function(producto){ return producto.inStock == false }); //ordenar productos disponibles productosDisponibles.sort((a, b) => a.price - b.price); //ordenar productos no disponibles productosNoDisponibles.sort((a, b) => a.price - b.price); //concatenar lista de productos let productosOrdenados = productosDisponibles.concat(productosNoDisponibles); return productosOrdenados; } const products = [ { name: "product1", price: 10, inStock: true }, { name: "product2", price: 20, inStock: false }, { name: "product3", price: 15, inStock: true }, { name: "product4", price: 5, inStock: false }, ] console.log(sortByAvailabilityAndPrice(products));
Array.prototype.filterInStock = function (inStock = true) { return this.filter(product => product.inStock === inStock); } Array.prototype.sortByPrice = function (ord = 'ASC') { return this.sort((a, b) => ord === 'ASC' ? a.price - b.price : b.price - a.price); } export const sortByAvailabilityAndPrice = (products) => { const stock = products.filterInStock().sortByPrice(); const outStock = products.filterInStock(false).sortByPrice(); return stock.concat(outStock); }
export function sortByAvailabilityAndPrice(products) { const array1 = [...products] .filter((e) => e.inStock === true) .sort((x, y) => x.price - y.price); const array2 = [...products] .filter((e) => e.inStock === false) .sort((x, y) => x.price - y.price); return array1.concat(array2); }
export function sortByAvailabilityAndPrice(products) { // Tu código aquí 👈 let listResult = [...products]; listResult.sort((a, b) => a.price - b.price); listResult.sort((a, b) => b.inStock - a.inStock); return listResult; }
export function sortByAvailabilityAndPrice(products) { const orderedProducts = [...products]; orderedProducts.sort((a, b) => a.price - b.price); orderedProducts.sort((a, b) => b.inStock - a.inStock) return orderedProducts; }
export function sortByAvailabilityAndPrice(products) { const newArray = [...products] newArray.sort((a, b) => a.price - b.price) newArray.sort((a, b) => b.inStock - a.inStock) return newArray; }