Skip to content Skip to sidebar Skip to footer

Js Sort Array Of Objets By Specific String Property (not Ascending Or Descending)

do you know if there is a way to sort an array of objects using the sort() method for a specific property? In my case I'd like to sort my array first for 'bananas', then for 'pears

Solution 1:

You could take an object with the wanted order and take a large value as default for sorting.

const
    data = [{ name: "strawberries", value: 12 }, { name: "bananas", value: 3 }, { name: "pears", value: 8 }, { name: "pears", value: 7 }, { name: "bananas", value: 10 }, { name: "apples", value: 6 }, { name: "bananas", value: 13 }, { name: "bananas", value: 5 }],
    order = { bananas: 1, pears: 2 };

data.sort((a, b) => (order[a.name] || Number.MAX_VALUE) - (order[b.name] || Number.MAX_VALUE));

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

Solution 2:

you can use an auxiliary array to define the sorting scheme. Here is an example:

const sortingScheme = [
  "bananas",
  "pears",
  "strawberries",
  "apples"
]

initialData.sort((a, b) => {
  const indexOfa = sortingScheme.indexOf(a.name);
  const indexOfb = sortingScheme.indexOf(b.name);
  if (indexOfa > indexOfb)
    return1if (indexOfa < indexOfb)
    return -1return0;
})

This only works if you know all the names the array can hold. If you are interested in ordering only a few fields you can do this:

const orderedScheme = [
  "bananas",
  "pears"
]

initialData.sort((a, b) => {
  let indexOfa = orderedScheme.indexOf(a.name);
  let indexOfb = orderedScheme.indexOf(b.name);

  if (indexOfa < 0) indexOfa = orderedScheme.length;
  if (indexOfb < 0) indexOfb = orderedScheme.length;

  if (indexOfa > indexOfb)
    return1if (indexOfa < indexOfb)
    return -1return0;
})

This is because if the element is not contained in the 'sortingScheme' array, the indexOf() function returns -1 which would place the element at the beginning of the array.

I hope I have been useful to you!

Solution 3:

You Can Use This For Custom Sorting:

initialData.sort(function(a, b) {
  var customSort="bpsacdefghijklmnoqrtuvwxyz";
  var aa=customSort.indexOf(a.substr(0,1));
  var bb=customSort.indexOf(b.substr(0,1));
  if (aa < bb) {
    return -1;
  }elseif (aa > bb) {
    return1;
  }elsereturn0;
});

Sorting Performs According to This Characters:

var customSort="bpsacdefghijklmnoqrtuvwxyz";

Solution 4:

sort method, check for names. when names are equal order by value. When names not equal then order by names. (check inline comments)

const initialData = [
  { name: "strawberries", value: 12 },
  { name: "bananas", value: 3 },
  { name: "pears", value: 8 },
  { name: "pears", value: 7 },
  { name: "bananas", value: 10 },
  { name: "apples", value: 6 },
  { name: "bananas", value: 13 },
  { name: "bananas", value: 5 },
];

initialData.sort((a, b) => {
  const order = ["pears", "bananas"]; // high priority items towards end.if (a.name === b.name && order.includes(a.name)) {
    return a.value - b.value;
  } else {
    // when item not in order, indexOf will return -1. // Basically comparing the values -1, 0, 1return order.indexOf(b.name) - order.indexOf(a.name);
  }
});

console.log(initialData);

Solution 5:

you can to this

const initialData = [
    { name: "strawberries", value: 12 },
    { name: "bananas", value: 3 },
    { name: "pears", value: 8 },
    { name: "pears", value: 7 },
    { name: "bananas", value: 10 },
    { name: "apples", value: 6 },
    { name: "bananas", value: 13 },
    { name: "bananas", value: 5 }
    ]

    const data = initialData.sort(function(a, b) {
        if (a.name < b.name) {
            if(a.name === "apples") {
                return1
            }
          return -1;
        }
        return0;
      });
      console.log(data)

Post a Comment for "Js Sort Array Of Objets By Specific String Property (not Ascending Or Descending)"