Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
How to latest data in array of objects database mongodb

I am trying to sort data in an array of objects. The problem is I don't have any date property so I can't be able to show the latest data,

I am trying to sort it like out this way but it is not working.

 const filter2 = data.sort((a, b) => (a > b ? -1 : 1))

Here is my data example

const data = [
        {
            _id: "6221943a03931299eb8c343b",
            title: "product 1",
            brand: "Beats",
            reg_price: "299",
            sale_price: "200",
            stock: "150",
        },
        {
            _id: "6221943a03931299eb8c343b",
            title: "product 2",
            brand: "Beats",
            reg_price: "299",
            sale_price: "200",
            stock: "150",
        },
        {
            _id: "6221943a03931299eb8c343b",
            title: "product 3",
            brand: "Beats",
            reg_price: "299",
            sale_price: "200",
            stock: "150",
        },
        {
            _id: "6221943a03931299eb8c343b",
            title: "product 4",
            brand: "Beats",
            reg_price: "299",
            sale_price: "200",
            stock: "150",
        },
        {
            _id: "6221943a03931299eb8c343b",
            title: "product 4",
            brand: "Beats",
            reg_price: "299",
            sale_price: "200",
            stock: "150",
        },
    ]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can do it like this if you want to sort it concerning object property

const data = [
        {
            _id: "6221943a03931299eb8c343b",
            title: "product 1",
            brand: "Beats",
            reg_price: "299",
            sale_price: "200",
            stock: "150",
        },
        {
            _id: "6221943a03931299eb8c343b",
            title: "product 2",
            brand: "Beats",
            reg_price: "299",
            sale_price: "200",
            stock: "150",
        },
        {
            _id: "6221943a03931299eb8c343b",
            title: "product 3",
            brand: "Beats",
            reg_price: "299",
            sale_price: "200",
            stock: "150",
        },
        {
            _id: "6221943a03931299eb8c343b",
            title: "product 4",
            brand: "Beats",
            reg_price: "299",
            sale_price: "200",
            stock: "150",
        },
        {
            _id: "6221943a03931299eb8c343b",
            title: "product 4",
            brand: "Beats",
            reg_price: "299",
            sale_price: "200",
            stock: "150",
        },
    ]

// sortProperty = '_id' | 'title' | 'brand' | 'reg_price' | 'sale_price' | 'stock'
const sortFunc = (sortArr,sortProperty) => sortArr.sort((a,b) => a[sortProperty] - b[sortProperty])

console.log(sortFunc(data,'_id'))


about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda