0

I have this data format in the below order. The data is corresponding to there employeeId

data={
1200: [
       {
            employeeName: Harish,
            task: task1,
            employeeId: '1200'
        },
        {
            employeename: Harish,
            task: task2,
            employeeId: '1200'
        },
        {
            employeename: Harish,
            task: task3,
            employeeId: '1200'
        }
 ]
1205: [
       {
            employeeName: Akshay,
            task: task1,
            employeeId: '1205'
        },
        {
            employeename: Akshay,
            task: task2,
            employeeId: '1205'
        },
],
1210: [
       {
            employeeName: Abhay,
            task: task1,
            employeeId: '1210'
        },
        {
            employeename: Abhay,
            task: task2,
            employeeId: '1210'
        },
]
}

I want the output to be sorted name wise and the output should be in the same format. for the above input the output should be

data={
1210: [
       {
            employeeName: Abhay,
            task: task1,
            employeeId: '1210'
        },
        {
            employeename: Abhay,
            task: task2,
            employeeId: '1210'
        },
]
1205: [
       {
            employeeName: Akshay,
            task: task1,
            employeeId: '1205'
        },
        {
            employeename: Akshay,
            task: task2,
            employeeId: '1205'
        },
],
   1200: [
       {
            employeeName: Harish,
            task: task1,
            employeeId: '1200'
        },
        {
            employeename: Harish,
            task: task2,
            employeeId: '1200'
        },
        {
            employeename: Harish,
            task: task3,
            employeeId: '1200'
        }
 ]
 }

I have tried like this

function sort() {
const data = {
1200: [
  {
    employeeName: 'Harish',
    task: 'task1',
    employeeId: '1200'
  },
  {
    employeeName: 'Harish',
    task: 'task2',
    employeeId: '1200'
  },
  {
    employeeName: 'Harish',
    task: 'task3',
    employeeId: '1200'
  },
],
1205: [
  {
    employeeName: 'Akshay',
    task: 'task1',
    employeeId: '1205'
  },
  {
    employeeName: 'Akshay',
    task: 'task2',
    employeeId: '1205'
  },
],
1210: [
  {
    employeeName: 'Abhay',
    task: 'task1',
    employeeId: '1210'
  },
  {
    employeeName: 'Abhay',
    task: 'task2',
    employeeId: '1210'
  },
 ],
};
  const sortedObj = {};
  Object.keys(data)
    .sort((a, b) => {
      const nameA = data[a][0].employeeName.toUpperCase();
      const nameB = data[b][0].employeeName.toUpperCase();
      if (nameA < nameB) return -1;
      if (nameA > nameB) return 1;
      return 0;
    })
    .forEach((key) => {
      sortedObj[key] = [...data[key]];
    });
  return sortedObj;
}
console.log(sort());

But this giving output like the original data

{
'1200': [
{ employeeName: 'Harish', task: 'task1', employeeId: '1200' },
{ employeeName: 'Harish', task: 'task2', employeeId: '1200' },
{ employeeName: 'Harish', task: 'task3', employeeId: '1200' }
],
'1205': [
{ employeeName: 'Akshay', task: 'task1', employeeId: '1205' },
{ employeeName: 'Akshay', task: 'task2', employeeId: '1205' }
],
'1210': [
{ employeeName: 'Abhay', task: 'task1', employeeId: '1210' },
{ employeeName: 'Abhay', task: 'task2', employeeId: '1210' }
]
}

Here is the sandbox link sandbox link

  • No, it *is* working. It's just that the console displays the keys in a sorted order. – kelsny Mar 01 '23 at 16:36
  • Which is why you shouldn't really use an object if there is a particular order of values you want. – kelsny Mar 01 '23 at 16:36
  • If you are using an object, why do you need to sort, anyway they are linked to a key. Also, @vr. is right, console will show the keys in a sorted order. – shivamragnar Mar 01 '23 at 16:41
  • But I want the output to sorted on the basis of employee names. isn't that possible here? @shivamragnar – user17320926 Mar 01 '23 at 16:44
  • It is sorted, just that you won't see it in the console. Try logging your sorted keys array. You will find that the array is sorted correctly. After that you do, forEach which is fine. The thing is console does not show it that way. – shivamragnar Mar 01 '23 at 16:46
  • You can check out here https://codesandbox.io/s/nostalgic-rosalind-pts3ns?file=/src/index.js – shivamragnar Mar 01 '23 at 16:47
  • The question is unclear. The title says you want to filter. The comments say you want to sort. What is it? Can you edit the question? Do you want to sort an object? – jabaa Mar 01 '23 at 17:05
  • Does this answer your question? [Does JavaScript guarantee object property order?](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) AFAIK there is no way to change the order of properties in an object with integer keys. – jabaa Mar 01 '23 at 17:06
  • your console shows the updated values but how can I get the sorted values in another object as I asked in question about the required output. Can't I get the same output?@shivamragnar – user17320926 Mar 02 '23 at 04:25
  • Please reread my comment. The order of properties with integer keys can't be changed. JavaScript guarantees a fixed order since 2015. You can find it in the duplicate. – jabaa Mar 02 '23 at 08:40

1 Answers1

-1

Short story long. To chew this down (see comments on the original question first), we can sort the Object for instance:

const data = {
1200: [
  {
    employeeName: 'Harish',
    task: 'task1',
    employeeId: '1200'
  },
  {
    employeeName: 'Harish',
    task: 'task2',
    employeeId: '1200'
  },
  {
    employeeName: 'Harish',
    task: 'task3',
    employeeId: '1200'
  },
],
1205: [
  {
    employeeName: 'Akshay',
    task: 'task1',
    employeeId: '1205'
  },
  {
    employeeName: 'Akshay',
    task: 'task2',
    employeeId: '1205'
  },
],
1210: [
  {
    employeeName: 'Abhay',
    task: 'task1',
    employeeId: '1210'
  },
  {
    employeeName: 'Abhay',
    task: 'task2',
    employeeId: '1210'
  },
 ],
};

const sorting = ({employeeName: a}, {employeeName: b}) => (a > b ? 1 : -1);

const sortedItems = Object.values(data).flatMap(val => val).sort(sorting);

console.log(sortedItems);

As you can see, the outcome is seven objects sorted by employeeName. As a sophisticated guess, this is probably the outcome you'd like to work with.

Now, in order to follow your desired structure, we need to reduce them back into single object with employeeId as the key in data:

const data = {
1200: [
  {
    employeeName: 'Harish',
    task: 'task1',
    employeeId: '1200'
  },
  {
    employeeName: 'Harish',
    task: 'task2',
    employeeId: '1200'
  },
  {
    employeeName: 'Harish',
    task: 'task3',
    employeeId: '1200'
  },
],
1205: [
  {
    employeeName: 'Akshay',
    task: 'task1',
    employeeId: '1205'
  },
  {
    employeeName: 'Akshay',
    task: 'task2',
    employeeId: '1205'
  },
],
1210: [
  {
    employeeName: 'Abhay',
    task: 'task1',
    employeeId: '1210'
  },
  {
    employeeName: 'Abhay',
    task: 'task2',
    employeeId: '1210'
  },
 ],
};


const sorting = ({employeeName: a}, {employeeName: b}) => (a > b ? 1 : -1);

const reduceBack = (acc, b) => ({...acc, [b.employeeId]: b});

const sortedData = Object.values(data).flatMap((val) => val).sort(sorting).reduce(reduceBack, {});


console.log(sortedData);

However, after all the effort, you still see the object sorted by keys. This is the way.

Samuli Hakoniemi
  • 18,740
  • 1
  • 61
  • 74