shilu
shilu

Reputation: 31

Array position by object value

I want to make a function that sorts array by the value of the specific key.

I will put example.

[{ text: 'hi', author: 'Boy' },
{ text: 'how are you', author: 'Boy' },
{ text: 'I\'m good', author: 'Boy' },
{ text: 'hello', author: 'Girl' },
{ text: 'Bye', author: 'Boy' }]

In the above array, 'Girl' author is more than 'Boy' author so it should return following array

[{ text: 'hello', author: 'Girl' },
{ text: 'hi', author: 'Boy' },
{ text: 'how are you', author: 'Boy' },
{ text: 'I\'m good', author: 'Boy' },
{ text: 'Bye', author: 'Boy' }]

Second example:

[{ text: 'hi', author: 'Boy' },
{ text: 'hola', author: 'Mom' },
{ text: 'how are you', author: 'Boy' },
{ text: 'I\'m good', author: 'Boy' },
{ text: 'hello', author: 'Girl' },
{ text: 'eat this', author: 'Mom' },
{ text: 'Bye', author: 'Boy' }]

Second result:

[{ text: 'hello', author: 'Girl' },
{ text: 'hola', author: 'Mom' },
{ text: 'eat this', author: 'Mom' },
{ text: 'hi', author: 'Boy' },
{ text: 'how are you', author: 'Boy' },
{ text: 'I\'m good', author: 'Boy' },
{ text: 'Bye', author: 'Boy' }]

Last example:

const data = [
  { text: 'hi', author: 'Boy' },
  { text: 'hola', author: 'Mom' },
  { text: 'hola', author: 'Mom' },
  { text: 'hola', author: 'Mom' },
  { text: 'how are you', author: 'Boy' },
  { text: "I'm good", author: 'Boy' },
  { text: 'hello', author: 'Girl' },
  { text: 'eat this', author: 'Mom' },
  { text: 'Bye', author: 'Boy' }
]

Last Result ( I don't care if Boy is first or Mom is first

const data = [
  { text: 'hello', author: 'Girl' },
  { text: 'hola', author: 'Mom' },
  { text: 'hola', author: 'Mom' },
  { text: 'hola', author: 'Mom' },
  { text: 'eat this', author: 'Mom' },
  { text: 'hi', author: 'Boy' },
  { text: 'how are you', author: 'Boy' },
  { text: "I'm good", author: 'Boy' },
  { text: 'Bye', author: 'Boy' }
]

Upvotes: 0

Views: 121

Answers (4)

Sifat Haque
Sifat Haque

Reputation: 6057

Here is the solution for this problem. You can use any property to sort this using this function.

console.clear();
const data = [{ text: 'hi', author: 'Boy' },
{ text: 'hola', author: 'Mom' },
{ text: 'how are you', author: 'Boy' },
{ text: 'I\'m good', author: 'Boy' },
{ text: 'hello', author: 'Girl' },
{ text: 'eat this', author: 'Mom' },
{ text: 'Bye', author: 'Boy' }];

const sortBy = (value, data) => {
  let sortedData = [];
  const countValueObj = data.reduce((acc, obj) => {
    const authorValue = obj[value];
    if(!acc[authorValue]) acc[authorValue] = 1;
    else acc[authorValue]++;
    return acc;
  }, {})
  const countValueArr = Object.keys(countValueObj).sort((a, b) => countValueObj[a] - countValueObj[b]);
  countValueArr.forEach((val) => {
    const filteredData = data.filter((obj) => obj[value] === val);
    sortedData = [...sortedData, ...filteredData];
  })
  return sortedData;
}

const output = sortBy('author', data);

console.log(output)

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386654

You could group by author and sort the keys by count and get a new array of objects.

var array = [{ text: 'hi', author: 'Boy' }, { text: 'hola', author: 'Mom' }, { text: 'how are you', author: 'Boy' }, { text: 'I\'m good', author: 'Boy' }, { text: 'hello', author: 'Girl' }, { text: 'eat this', author: 'Mom' }, { text: 'Bye', author: 'Boy' }],
    temp = array.reduce((r, o) => {
        if (!r[o.author]) r[o.author] = [];
        r[o.author].push(o);
        return r;
    }, {}),
    result = Object
        .keys(temp)
        .sort((a, b) => temp[a].length - temp[b].length)
        .flatMap(k => temp[k]);

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

Upvotes: 0

Prateek Sharma
Prateek Sharma

Reputation: 67

If you modify your dataset and add a count to it, the following code should do the job.

var data = [{ text: 'hi', author: 'Boy' },
{ text: 'hola', author: 'Mom' },
{ text: 'how are you', author: 'Boy' },
{ text: 'I\'m good', author: 'Boy' },
{ text: 'hello', author: 'Girl' },
{ text: 'eat this', author: 'Mom' },
{ text: 'Bye', author: 'Boy' }];

// add counts against each object
data.forEach(obj => {
  obj['count'] = data.filter((obj1) => obj1.author === 
    obj.author).length;
})

// user Array.sort function to sort your data
data.sort(function(a, b){
    if(a.count < b.count) return -1;
    if(a.count > b.count) return 1;
    return 0;
});

Although, it would be better if you get this list sorted from the backend.

Upvotes: 1

Mufaddal Hamid
Mufaddal Hamid

Reputation: 357

You could use

array.sort((a, b) => {
  return a.author - b.author;
})

Upvotes: 0

Related Questions