Home > database >  how can I ordenate a object array based an array order?
how can I ordenate a object array based an array order?

Time:09-23

I have the following array of objects:

const data = [
  { food: 'dessert', name: 'sagu de tangerina' },
  { food: 'garnish2', name: 'moranga refogada com bacon' },
  { food: 'salad2', name: 'pimentão colorido' },
  { food: 'rice', name: 'arroz' },
  { food: 'brownRice', name: 'arroz integral' },
  { food: 'salad4', name: 'beterraba ralada' },
  { food: 'salad3', name: 'agrião com manga' },
  { food: 'meat2', name: 'filezinho de frango empanado' },
  { food: 'cutFruit', name: 'melancia' },
  { food: 'meat1', name: 'dobradinha' },
  { food: 'salad1', name: 'escarola frisse' },
  { food: 'bean', name: 'feijão' },
  { food: 'garnish1', name: 'batata rustica II' },
  { food: 'salad5', name: 'chucrute caseiro' },
  { food: 'meat3', name: 'ovo assado' }
]

I need to sort in foods order:

let orderBy = ["salad", "cutFruit", "dessert", "syrup", "coldSauce", "hotSauce", "garnish", "rice", "bean", "meat"];

My attempt:

let orderBy = ["salad", "cutFruit", "dessert", "syrup", "coldSauce", "hotSauce", "garnish", "rice", "bean", "meat"];
data.sort((a, b) => {
  let fa = a.food.toLowerCase(),
    fb = b.food.toLowerCase();

  orderBy.forEach(function(order, i) {
    if (fa.includes(orderBy[i]) && fb.includes(orderBy[i   1])) {
      return -1;
    } else if (fa.includes(orderBy[i   1]) && fb.includes(orderBy[i])) {
      return 1;
    } else {
      return 0;
    }
  })

  // return fa < fb ? -1 : fa > fb ? 1 : 0;
});

I found the solution for this:

food.sort((a, b) => {
    let fa = a.food.toLowerCase();
    let fb = b.food.toLowerCase();

    return fa < fb ? -1 : fa > fb ? 1 : 0;
});

let orderBy = ["salad", "coldSauce", "hotSauce", "garnish", "rice", "brownRice", "bean", "meat", "dessert", "syrup", "cutFruit", "juice"];
const foodOrderly= [];

orderBy.forEach(function (order) {
    food.forEach(function (point) {
        if (point.food.includes(order)) {
            foodOrderly.push(point);
        }
    });
}); 

CodePudding user response:

const data = [
  { food: 'dessert', name: 'sagu de tangerina' },
  { food: 'garnish2', name: 'moranga refogada com bacon' },
  { food: 'salad2', name: 'pimentão colorido' },
  { food: 'rice', name: 'arroz' },
  { food: 'brownRice', name: 'arroz integral' },
  { food: 'salad4', name: 'beterraba ralada' },
  { food: 'salad3', name: 'agrião com manga' },
  { food: 'meat2', name: 'filezinho de frango empanado' },
  { food: 'cutFruit', name: 'melancia' },
  { food: 'meat1', name: 'dobradinha' },
  { food: 'salad1', name: 'escarola frisse' },
  { food: 'bean', name: 'feijão' },
  { food: 'garnish1', name: 'batata rustica II' },
  { food: 'salad5', name: 'chucrute caseiro' },
  { food: 'meat3', name: 'ovo assado' }
]

let orderBy = ["salad", "cutFruit", "dessert", "syrup", "coldSauce", "hotSauce", "garnish", "rice", "bean", "meat"];

// a RegExp to seperate the group from the index.
const r = /^([a-z] )(\d*)/i;

const sorted = data.slice().sort(({ food: a }, { food: b }) => {
  // match the group and the index
  const aa = a.match(r);
  const bb = b.match(r);

  // same group, sort by index
  if (aa[1] === bb[1]) {
    return aa[2] - bb[2];
  }

  // sort by index in orderBy
  return orderBy.indexOf(aa[1]) - orderBy.indexOf(bb[1]);
});

console.log(sorted);
// brownRice has an index of -1 and is therefore sorted to the top.
.as-console-wrapper{top:0;max-height:100%!important}

CodePudding user response:

Took ideas from answers that didn't fully work and managed to get a simpler solution:

const data = [
  { food: 'dessert', name: 'sagu de tangerina' },
  { food: 'garnish2', name: 'moranga refogada com bacon' },
  { food: 'salad2', name: 'pimentão colorido' },
  { food: 'rice', name: 'arroz' },
  { food: 'brownRice', name: 'arroz integral' },
  { food: 'salad4', name: 'beterraba ralada' },
  { food: 'salad3', name: 'agrião com manga' },
  { food: 'meat2', name: 'filezinho de frango empanado' },
  { food: 'cutFruit', name: 'melancia' },
  { food: 'meat1', name: 'dobradinha' },
  { food: 'salad1', name: 'escarola frisse' },
  { food: 'bean', name: 'feijão' },
  { food: 'garnish1', name: 'batata rustica II' },
  { food: 'salad5', name: 'chucrute caseiro' },
  { food: 'meat3', name: 'ovo assado' }
]

const orderBy = ["salad", "cutFruit", "dessert", "syrup", "coldSauce", "hotSauce", "garnish", "rice", "brownRice", "bean", "meat"];

data.sort((a, b) => {
  return value(a) > value(b)
})

function value(item) {
  return orderBy.indexOf(item.food.replace(/[0-9]/g, ''))
}

console.log(data)
.as-console-wrapper { top: 0; max-height: 100%!important}

  • Related