0

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' }
]
5
  • Does this answer your question? Sort array of objects by string property value Commented Mar 15, 2020 at 12:32
  • do you want to sort by count of the authors? Commented Mar 15, 2020 at 12:34
  • can you clarify a bit? what will be the output of this input `[{ text: 'hi', author: 'Boy' }, { text: 'hola', author: 'Mom' }, { text: 'how are you', author: 'Boy' }, { text: 'eat this', author: 'Mom' } Commented Mar 15, 2020 at 12:46
  • @shilu then what if there is same number of authors? Commented Mar 15, 2020 at 12:56
  • @shilu ok then, please check my answer to your post. Commented Mar 15, 2020 at 13:07

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@shilu Can you provide dataset you tested with?
0

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; }

Comments

0

You could use

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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.