No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Sort

27/28
Recursos

Aportes 41

Preguntas 2

Ordenar por:

Los aportes, preguntas y respuestas son vitales para aprender en comunidad. Regístrate o inicia sesión para participar.

Les dejo los arrays para que no tengan que escribir:

const months = ["March", "Jan", "Feb", "Dec"];

const numbers = [1, 30, 4, 21, 100000];

const words = [
    "réservé",
    "premier",
    "communiqué",
    "café",
    "adieu",
    "éclair",
    "banana",
];

const orders = [
    {
        customerName: "Nicolas",
        total: 600,
        delivered: true,
    },
    {
        customerName: "Zulema",
        total: 120,
        delivered: false,
    },
    {
        customerName: "Santiago",
        total: 1840,
        delivered: true,
    },
    {
        customerName: "Valentina",
        total: 240,
        delivered: true,
    },
];

¿Por qué a - b o b - a?

La función que le enviamos a sort es la función compareFn donde:

  • Si compareFn(a, b) devuelve un valor mayor que 0, ordena b antes a.
  • Si compareFn(a, b) devuelve un valor menor que 0, ordena a antes b.
  • Si compareFn(a, b) devuelve 0 a y b se consideran iguales.

Para ordenar los meses

Creé una función que le de un valor numérico a cada mes y luego le envié esa función a Sort:

const months = ['Febrero', 'Julio', "fsdf", 'Diciembre', 'Enero'];
function monthValue(month) {
    switch (month.toUpperCase()) {
        case 'ENERO':
            return 1;
        case 'FEBRERO':
            return 2;
        case 'MARZO':
            return 3;
        case 'ABRIL':
            return 4;
        case 'MAYO':
            return 5;
        case 'JUNIO':
            return 6;
        case 'JULIO':
            return 7;
        case 'AGOSTO':
            return 8;
        case 'SEPTIEMBRE':
            return 9;
        case 'OCTUBRE':
            return 10;
        case 'NOVIEMBRE':
            return 11;
        case 'DICIEMBRE':
            return 12;
        default:
            //Cualquier valor que no coincida se irá de último
            return 13; 
    }
}

months.sort((a, b) => monthValue(a) - monthValue(b))

console.log(months);

Resultado:

[ 'Enero', 'Febrero', 'Julio', 'Diciembre', 'fsdf' ]

Reto

const orders = [
    {
      customerName: "Nicolas",
      total: 600,
      delivered: true,
      date: new Date (2021, 3, 8, 4),
    },
    {
      customerName: "Zulema",
      total: 120,
      delivered: false,
      date: new Date (2021, 9, 12, 3),
    },
    {
      customerName: "Santiago",
      total: 1840,
      delivered: true,
      date: new Date (2021, 8, 2, 2),
    },
    {
      customerName: "Valentina",
      total: 240,
      delivered: true,
      date: new Date(2021, 1, 1, 9, 30),
    },
  ];
  orders.sort((a,b) => a.date - b.date);
  console.log(orders);
console.clear();

const months = ['March', 'Jan', 'Feb', 'Dec'];

const numbers = [1, 30, 4, 21, 100000];

const words = ['réservé', 'premier', 'communiqué', 'café', 'adieu', 'éclair', 'banana'];

const orders = [
  {
    customerName: 'Nicolas',
    total: 600,
    delivered: true,
    date: new Date(2021, 1, 1, 15),
  },
  {
    customerName: 'Zulema',
    total: 120,
    delivered: false,
    date: new Date(2021, 1, 1, 10),
  },
  {
    customerName: 'Santiago',
    total: 1840,
    delivered: true,
    date: new Date(2021, 1, 1, 20),
  },
  {
    customerName: 'Valentina',
    total: 240,
    delivered: true,
    date: new Date(2021, 1, 1, 8),
  },
];

months.sort();
console.log({ months });

numbers.sort();
console.log({ numbers });

numbers.sort((a, b) => a - b);
console.log({ numbers });

numbers.sort((a, b) => b - a);
console.log({ numbers });

words.sort();
console.log({ words });

words.sort((a, b) => a.localeCompare(b));
console.log({ words });

orders.sort((a, b) => a.total - b.total);
console.log({ orders });

orders.sort((a, b) => b.total - a.total);
console.log({ orders });

orders.sort((a, b) => b.date.getTime() - a.date.getTime());
console.log({ orders });

RETO: usando .getTime() en la fecha

orders.sort((a, b) => b.date.getTime() - a.date.getTime());
console.log(orders);

Mi solución al reto:

const orders = [
    {
      customerName: "Nicolas",
      total: 600,
      delivered: true,
      datePurchased: '2021-10-28 12:00:00'
    },
    {
      customerName: "Zulema",
      total: 120,
      delivered: false,
      datePurchased: '2021-10-29 12:00:00'
    },
    {
      customerName: "Santiago",
      total: 1840,
      delivered: true,
      datePurchased: '2021-10-30 12:00:00'
    },
    {
      customerName: "Valentina",
      total: 240,
      delivered: true,
      datePurchased: '2021-10-31 12:00:00'
    },
  ];
  orders.sort((a,b) => new Date(b.datePurchased) - new Date(a.datePurchased));
  console.log(orders);

Reto izi pizi jajaja que gran curso!

const orders = [
    {
        customerName: 'Nicolas',
        total: 600,
        delivered: true,
        date: new Date(2021, 1, 1, 15),
    },
    {
        customerName: 'Zulema',
        total: 120,
        delivered: false,
        date: new Date(2021, 1, 1, 10),
    },
    {
        customerName: 'Santiago',
        total: 1840,
        delivered: true,
        date: new Date(2021, 1, 1, 20),
    },
    {
        customerName: 'Valentina',
        total: 240,
        delivered: true,
        date: new Date(2021, 1, 1, 8),
    },
];
const ordersSorted = orders.sort((a, b) => {
    return a.date.getTime() - b.date.getTime();
});

console.log(ordersSorted);

mi solución del reto 🤗🤗🤗:

Comparar fechas JS

Mi respuesta al reto:

const ordersWDate = [
    {
        customerName: "Nicolas",
        total: 600,
        delivered: true,
        date: new Date(2022, 1, 14, 15, 45)
    },
    {
        customerName: "Zulema",
        total: 120,
        delivered: false,
        date: new Date(2021, 11, 18, 18)
    },
    {
        customerName: "Santiago",
        total: 1840,
        delivered: true,
        date: new Date(2021, 11, 24, 20, 30)
    },
    {
        customerName: "Valentina",
        total: 240,
        delivered: true,
        date: new Date(2022, 0, 18, 14, 15)
    },
];
ordersWDate.sort((a, b) => b.date - a.date)
console.log(ordersWDate)

Para ordenar los meses por su orden correspondiente 📅

const months = [
  {
    mes: "March",
    id: 03,
  },
  {
    mes: "Jan",
    id: 01,
  },
  {
    mes: "Feb",
    id: 02,
  },
  {
    mes: "Dec",
    id: 12,
  },
];
const meses = months.sort((a, b) => a.id - b.id).map((item) => item.mes);
console.log(meses);

Para ordenar por fechas es tan simple como usar el nombre del objeto y su propiedad date.

const orders = [
  {
    customerName: "Nicolas",
    total: 600,
    delivered: true,
    date: new Date(2019, 0, 2),
  },
  {
    customerName: "Zulema",
    total: 120,
    delivered: false,
    date: new Date(2019, 10, 1),
  },
  {
    customerName: "Santiago",
    total: 1840,
    delivered: true,
    date: new Date(2020, 4, 1),
  },
  {
    customerName: "Valentina",
    total: 240,
    delivered: true,
    date: new Date(2021, 3, 6)
  },
];

console.log(orders.sort((a, b) => b.total - a.total));
console.log(orders.sort((a,b) => b.date - a.date));
const orders = [
  {
    customerName: "Nicolas",
    total: 600,
    delivered: true,
    datePurchased: "2022-01-12 12:00:00",
  },
  {
    customerName: "Zulema",
    total: 120,
    delivered: false,
    datePurchased: "2022-01-20 12:00:00",
  },
  {
    customerName: "Santiago",
    total: 1840,
    delivered: true,
    datePurchased: "2022-01-01 12:00:00",
  },
  {
    customerName: "Valentina",
    total: 240,
    delivered: true,
    datePurchased: "2022-01-15 12:00:00",
  },
];
orders.sort((a, b) => new Date(b.datePurchased) - new Date(a.datePurchased));
console.log(orders);

Mi solución para ordenar fechas:

orders.sort((a,b) => a.orderDate - b.orderDate);

buenas noches,

comparto reto.

const orders = [
    {
        customerName: "Nicolas",
        total: 600,
        delivered: true,
        date:new Date(2022, 2, 22, 12)
    },
    {
        customerName: "Zulema",
        total: 120,
        delivered: false,
        date:new Date(2022, 6, 1, 12)
    },
    {
        customerName: "Santiago",
        total: 1840,
        delivered: true,
        date:new Date(2022, 1, 1, 12)
    },
    {
        customerName: "Valentina",
        total: 240,
        delivered: true,
        date:new Date(2022, 5, 5, 12)
    },
];

orders.sort((a,b) => b.date - a.date);
console.log(orders);

Gracias.

Agregué un campo deliverDate a cada order

orders.sort((a, b) => b.deliverDate - a.deliverDate)

RETO:

const orders = [
  {
    customerName: "Nicolas",
    total: 600,
    delivered: true,
    date: new Date(2022, 0, 1, 15, 30),
  },
  {
    customerName: "Zulema",
    total: 120,
    delivered: false,
    date: new Date(2022, 0, 1, 10),
  },
  {
    customerName: "Santiago",
    total: 1840,
    delivered: true,
    date: new Date(2021, 11, 31, 15),
  },
  {
    customerName: "Valentina",
    total: 240,
    delivered: true,
    date: new Date(2022, 1, 1, 18),
  },
];
orders.sort((a, b) => b.date-a.date);
console.log(orders);

Reto

const orders = [
    {
      customerName: "Nicolas",
      total: 600,
      delivered: true,
      date: new Date(2021, 1, 5, 15),
    },
    {
      customerName: "Zulema",
      total: 120,
      delivered: false,
      date: new Date(2021, 1, 10, 18),
    },
    {
      customerName: "Santiago",
      total: 1840,
      delivered: true,
      date: new Date(2021, 1, 7, 10),
    },
  ];
  orders.sort((a,b) => b.date - a.date);
  console.log(orders);

Para ordenar los meses en forma de string 😎 esto se podria hacer asi:

const months = {
    3: 'Marzo',
    7: 'Julio',
    4: 'Abril',
    12: 'Diciembre',
    1: 'Enero'
}
Object.keys(months).sort((a,b) => a-b).map(monthNum => months[monthNum]); // ['Enero', 'Marzo', 'Abril', 'Julio', 'Diciembre']

Reto cumplido

const orders2 = [
  {
    customerName: "Nicolas",
    total: 600,
    delivered: true,
    date: new Date (2021, 3, 8, 4),
  },
  {
    customerName: "Zulema",
    total: 120,
    delivered: false,
    date: new Date (2021, 9, 12, 3),
  },
  {
    customerName: "Santiago",
    total: 1840,
    delivered: true,
    date: new Date (2021, 8, 2, 2),
  },
  {
    customerName: "Valentina",
    total: 240,
    delivered: true,
    date: new Date(2022, 1, 1, 9, 30),
  },
];

const sortDate = orders2.sort((a,b) => b.date - a.date)
console.log(sortDate);

Reto ordenar por fechas

const orders = [
    {
        customerName: "Nicolas",
        total: 600,
        delivered: true,
        date: new Date(2020,12,22,2)
    },
    {
        customerName: "Zulema",
        total: 120,
        delivered: false,
        date: new Date(2021, 9, 12, 3)
    },
    {
        customerName: "Santiago",
        total: 1840,
        delivered: true,
        date:new Date(2021,6,11,4)
    },
    {
        customerName: "Valentina",
        total: 240,
        delivered: true,
        date:new Date(2022,06,11,2)
    },
];
orders.sort((a,b) => b.date-a.date);
console.log('Orden por fecha',orders);

Reto del final de la clase

const orders = [
    {
        customerName: "Nicolas",
        total: 600,
        delivered: true,
        date: new Date (2022, 3, 6)
    },
    {
        customerName: "Zulema",
        total: 120,
        delivered: false,
        date: new Date (2022, 3, 2)
    },
    {
        customerName: "Santiago",
        total: 1840,
        delivered: true,
        date: new Date (2022, 6, 3)
    },
    {
        customerName: "Valentina",
        total: 240,
        delivered: true,
        date: new Date (2022, 4, 1)
    },
];
orders.sort((a,b) => a.date - b.date);
console.log(orders);

Output

[
  {
    customerName: 'Zulema',
    total: 120,
    delivered: false,
    date: 2022-04-02T03:00:00.000Z
  },
  {
    customerName: 'Nicolas',
    total: 600,
    delivered: true,
    date: 2022-04-06T04:00:00.000Z
  },
  {
    customerName: 'Valentina',
    total: 240,
    delivered: true,
    date: 2022-05-01T04:00:00.000Z
  },
  {
    customerName: 'Santiago',
    total: 1840,
    delivered: true,
    date: 2022-07-03T04:00:00.000Z
  }
]

Reto:

const orders = [
    {
        customerName: "Nicolas",
        total: 600,
        delivered: true,
        date: new Date(2020,5,18)
    },
    {
        customerName: "Zulema",
        total: 120,
        delivered: false,
        date: new Date(2021,5,18)
    },
    {
        customerName: "Santiago",
        total: 1840,
        delivered: true,
        date: new Date(2022,5,18)
    },
    {
        customerName: "Valentina",
        total: 240,
        delivered: true,
        date: new Date(2023,5,18)
    },
];

orders.sort((a,b) => a.date - b.date);
console.log(orders);

Si quiero ordenar desde la más antigua primero; invierto los valores a.date y b.date en el sort()

Note✏️: “js ordena de acuerdo al código ascii”

Reto:

const orders = [
    {
        customerName: "Nicolas",
        total: 600,
        delivered: true,
        date: new Date (2022, 1, 5, 10).toString()
    },
    {
        customerName: "Zulema",
        total: 120,
        delivered: false,
        date: new Date (2022, 2, 5, 12).toString()   
    },
    {
        customerName: "Santiago",
        total: 1840,
        delivered: true,
        date: new Date (2022, 3, 5, 14).toString()
    },
    {
        customerName: "Valentina",
        total: 240,
        delivered: true,
        date: new Date (2022, 4, 5, 16).toString()
    },
];

orders.sort((a,b) => a.date - b.date);
console.log(orders);

My solution to the challenge

function randomDate(start, end) {
  return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()))
}

orders
.map((order) => order.date = randomDate(new Date(2022,05,23), new Date()))
.sort((order, order2)=> order.getTime() - order2.getTime() )

XD


const orders = [
    {
        customerName: "Nicolas",
        total: 600,
        delivered: true,
        date: new Date(2022,7,2,2)
    },
    {
        customerName: "Zulema",
        total: 120,
        delivered: false,
        date: new Date(2022,6,4,2)
    },
    {
        customerName: "Santiago",
        total: 1840,
        delivered: true,
        date: new Date(2022,8,10,1)
    },
    {
        customerName: "Valentina",
        total: 240,
        delivered: true,
        date: new Date(2022,5,1,6)
    },
];
// orders.sort((a,b)=>b.total - a.total);
// console.log(orders);

//RETO
orders.sort((a,b) => b.date - a.date);
console.log(orders)

**RETO **

-📅 solución al sort de los meses

const months = ["March", "Jan", "Feb", "Dec", "Jan"];
months.sort((a, b) => {
    return (
        new Date(`${a} 2022`) - new Date(`${b} 2022`)
    )
});
console.log(months);
  • 😊Reto
const orders = [
    {
        customerName: "Nicolas",
        total: 600,
        delivered: true,
        date: new Date(2020, 0, 5),
    },
    {
        customerName: "Zulema",
        total: 120,
        delivered: false,
        date: new Date(2020, 0, 2),
    },
    {
        customerName: "Santiago",
        total: 1840,
        delivered: true,
        date: new Date(2020, 0, 6),
    },
    {
        customerName: "Valentina",
        total: 240,
        delivered: true,
        date: new Date(2020, 0, 4),
    },
];
orders.sort((a, b) => a.date - b.date);
console.table(orders);

RETO

Challenge completed

Les comparto la solución al reto. 😄

const orders = 
[
    {
        customerName: "Nicolas",
        total: 600,
        delivered: true,
    },
    {
        customerName: "Zulema",
        total: 120,
        delivered: false,
    },
    {
        customerName: "Santiago",
        total: 1840,
        delivered: true,
    },
    {
        customerName: "Valentina",
        total: 240,
        delivered: true,
    },
];

const ordersV2 = orders
    .map(
        (item, index) => {
            return {
                ...item,
                date: new Date(2022, index, 2)
            };
        }
    )
    .sort((a, b) => b.date - a.date);

console.log({ordersV2});

const orders = [
  {
    customerName: "Nicolas",
    total: 600,
    delivered: true,
    fechas:"2019-10-29",
  },
  {
    customerName: "Zulema",
    total: 120,
    delivered: false,
    fechas:"2017-10-27",
  },
  {
    customerName: "Santiago",
    total: 1840,
    delivered: true,
    fechas:"2019-10-26",
  },
  {
    customerName: "Valentina",
    total: 240,
    delivered: true,
    fechas:"2018-10-25",
  },
];

orders.sort((a,b) => new Date(a.fechas).getTime() - new Date(b.fechas).getTime() );
console.log(orders)

Solución al reto

const orders = [
    {
        customerName: "Nicolas",
        total: 600,
        delivered: true,
        dateDelivered: new Date(2022, 02, 26)
    },
    {
        customerName: "Zulema",
        total: 120,
        delivered: false,
        dateDelivered: new Date(2020, 04, 11)
    },
    {
        customerName: "Santiago",
        total: 1840,
        delivered: true,
        dateDelivered: new Date(2021, 10, 05)
    },
    {
        customerName: "Valentina",
        total: 240,
        delivered: true,
        dateDelivered: new Date(2022, 05, 30)
    },
];

orders.sort((a, b) => b.dateDelivered - a.dateDelivered)
console.log(orders)
const orders = [
    {
        customerName: "Nicolas",
        total: 600,
        delivered: true,
        date: new Date(2022, 02, 23)
    },
    {
        customerName: "Zulema",
        total: 120,
        delivered: false,
        date: new Date(2022, 02, 21)
    },
    {
        customerName: "Santiago",
        total: 1840,
        delivered: true,
        date: new Date(2022, 02, 22)
    },
    {
        customerName: "Valentina",
        total: 240,
        delivered: true,
        date: new Date(2022, 02, 20)
    },
];

orders.sort((a, b) => a.date.getTime() - b.date.getTime())
console.log(orders)

Reto 😄


const orders = [
  {
    customerName: 'Nicolas',
    
    date: new Date(2021, 1, 1, 15),
  },
  {
    customerName: 'Zulema',
    
    date: new Date(2021, 1, 1, 10),
  },
  {
    customerName: 'Santiago',
    
    date: new Date(2021, 1, 1, 20),
  },
  {
    customerName: 'Valentina',
   
    date: new Date(2021, 1, 1, 8),
  },
];

Resultado








[
  {
    customerName: 'Nicolas',
    date: 2021-02-01T21:00:00.000Z
  },
  {
    customerName: 'Zulema',
    date: 2021-02-01T16:00:00.000Z
  },
  {
    customerName: 'Santiago',
    date: 2021-02-02T02:00:00.000Z
  },
  {
    customerName: 'Valentina',
    date: 2021-02-01T14:00:00.000Z
  }
]
{
  orders: [
    {
      customerName: 'Nicolas',
      date: 2021-02-01T21:00:00.000Z
    },
    {
      customerName: 'Zulema',
      date: 2021-02-01T16:00:00.000Z
    },
    {
      customerName: 'Santiago',
      date: 2021-02-02T02:00:00.000Z
    },
    {
      customerName: 'Valentina',
      date: 2021-02-01T14:00:00.000Z
    }
  ]
}
[
  {
    customerName: 'Nicolas',
    date: 2021-02-01T21:00:00.000Z
  },
  {
    customerName: 'Zulema',
    date: 2021-02-01T16:00:00.000Z
  },
  {
    customerName: 'Santiago',
    date: 2021-02-02T02:00:00.000Z
  },
  {
    customerName: 'Valentina',
    date: 2021-02-01T14:00:00.000Z
  }
]
{
  orders: [
    {
      customerName: 'Nicolas',
      date: 2021-02-01T21:00:00.000Z
    },
    {
      customerName: 'Zulema',
      date: 2021-02-01T16:00:00.000Z
    },
    {
      customerName: 'Santiago',
      date: 2021-02-02T02:00:00.000Z
    },
    {
      customerName: 'Valentina',
      date: 2021-02-01T14:00:00.000Z
    }
  ]
}
[
  {
    customerName: 'Santiago',
    date: 2021-02-02T02:00:00.000Z
  },
  {
    customerName: 'Nicolas',
    date: 2021-02-01T21:00:00.000Z
  },
  {
    customerName: 'Zulema',
    date: 2021-02-01T16:00:00.000Z
  },
  {
    customerName: 'Valentina',
    date: 2021-02-01T14:00:00.000Z
  }
]
{
  orders: [
    {
      customerName: 'Santiago',
      date: 2021-02-02T02:00:00.000Z
    },
    {
      customerName: 'Nicolas',
      date: 2021-02-01T21:00:00.000Z
    },
    {
      customerName: 'Zulema',
      date: 2021-02-01T16:00:00.000Z
    },
    {
      customerName: 'Valentina',
      date: 2021-02-01T14:00:00.000Z
    }
  ]
}

Ordenar los meses:

const months = ['March', 'Jan', 'Dec', 'Feb', 'Jul', 'Jun'];

const calendar = {
	Jan: 1,
	Feb: 2,
	March: 3,
	April: 4,
	May: 5,
	Jun: 6,
	Jul: 7,
	Dec: 12,
};

const orderMonths = (array, sample) => {
	array.sort((a, b) => sample[a] - sample[b]);
};

orderMonths(months, calendar);
console.log(months);

Acá les dejo mi solución de .sort(),

const orders = [
  {
    customerName: 'Nicolas',
    total: 600,
    delivered: true,
    date: new Date(2021, 0, 25, 13,30),
  },
  {
    customerName: 'Zulema',
    total: 120,
    delivered: false,
    date: new Date(2021, 6, 25, 15, 40),
  },
  {
    customerName: 'Santiago',
    total: 1840,
    delivered: true,
    date: new Date(2021, 2, 25, 16, 35),
  },
]
orders.sort((a,b) => a.date - b.date)
console.log(orders)

Mi solución al reto.

orders.sort((a, b) => Date.parse(a.date) - Date.parse(b.date))

**Date.parse( [fecha] ) **: Lo que hace es que transforma una cadena con la representación de una fecha y hora, y devuelve el número de milisegundos desde las 00:00:00 del 1 de enero de 1970, hora local.

const months = [
    {
        month:"March",
        date: 3
    },
    {
        month:"Jan",
        date: 1
    },
    {
        month:"Feb",
        date: 2
    },
    {
        month:"Dec",
        date: 12
    }
];
months.sort((a,b) => a.date - b.date);
console.log(months)