0

I'm having a problem with sorting an object.

Lets say my array looks like this:

[{name:'a', type: 'letter', date:'30897887', active: true},
{name:'b', type: 'letter', date:'30897888', active: true},
{name:'c', type: 'number', date:'30897889', active: true},
{name:'d', type: 'letter', date:'30897890', active: false},
{name:'e', type: 'letter', date:'30897895', active: true},
{name:'f', type: 'number', date:'30897878', active: false},
{name:'g', type: 'letter', date:'30897823', active: true},
{name:'h', type: 'idk', date:'30897885456', active: true}]


I need to show the active and the not active ones by toggling the view, in this I've been successful.
What's driving me insane is: I need to group them by type with a specific order. I cannot wrap my head around a method.
Let's say the order should be "all the objects with type = number first, then idk, and letter at the end.".
How can i do it?

4 Answers 4

1

It isn't so elegant but here is an example if it can help you :

const array = [{name:'a', type: 'letter', date:'30897887', active: true},
{name:'b', type: 'letter', date:'30897888', active: true},
{name:'c', type: 'number', date:'30897889', active: true},
{name:'d', type: 'letter', date:'30897890', active: false},
{name:'e', type: 'letter', date:'30897895', active: true},
{name:'f', type: 'number', date:'30897878', active: false},
{name:'g', type: 'letter', date:'30897823', active: true},
{name:'h', type: 'idk', date:'30897885456', active: true}];

const types = ['idk', 'letter', 'number'];

let newArray = [];

types.forEach(type => {
  array.forEach(item => {
    if (item.type === type) {
      newArray = [...newArray, item];
    }
  })
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can use priority mapping and then sort the array using that map like below :

let priority = {
  "letter": 1,
  "idk": 2,
  "number": 3
};

arr.sort((a, b) => priority[a.type] - priority[b.type])

const arr = [{
    name: 'a',
    type: 'letter',
    date: '30897887',
    active: true
  },
  {
    name: 'b',
    type: 'letter',
    date: '30897888',
    active: true
  },
  {
    name: 'c',
    type: 'number',
    date: '30897889',
    active: true
  },
  {
    name: 'd',
    type: 'letter',
    date: '30897890',
    active: false
  },
  {
    name: 'e',
    type: 'letter',
    date: '30897895',
    active: true
  },
  {
    name: 'f',
    type: 'number',
    date: '30897878',
    active: false
  },
  {
    name: 'g',
    type: 'letter',
    date: '30897823',
    active: true
  },
  {
    name: 'h',
    type: 'idk',
    date: '30897885456',
    active: true
  }
]

let priority = {
  "letter": 1,
  "idk": 2,
  "number": 3
};

arr.sort((a, b) => priority[a.type] - priority[b.type])
console.log(arr)

 priority = {
  "letter": 3,
  "idk": 2,
  "number": 1
};

arr.sort((a, b) => priority[a.type] - priority[b.type])
console.log(arr)
.as-console-wrapper {height:100%}

Comments

0

You can use compare function to define custom sorting order

Try this:

var items = [
{name:'a', type: 'letter', date:'30897887', active: true},
{name:'b', type: 'letter', date:'30897888', active: true},
{name:'c', type: 'number', date:'30897889', active: true},
{name:'d', type: 'letter', date:'30897890', active: false},
{name:'e', type: 'letter', date:'30897895', active: true},
{name:'f', type: 'number', date:'30897878', active: false},
{name:'g', type: 'letter', date:'30897823', active: true},
{name:'h', type: 'idk', date:'30897885456', active: true}
];

items.sort((item1,item2)=>{
 if(item1.type === 'number'){
 return -1;  
}
if(item2.type === 'number'){
 return 1;  
}
if(item1.type === 'idk'){
 return -2;  
}
if(item2.type === 'idk'){
 return 2;  
}
});

console.log(items);

Comments

0
function customSort(arr, order) {
  var result = [];
  order.forEach((type) => {
      result = [...result,...arr.filter((data) => data.type === type)]
    });
  return result
}

Use it like

customSort(data, ["letter", "number", "string"])

4 Comments

How does this reflect the specific order?
It is sorting on the basis of type in ascending order.
I do not need ascending order, i need the array to be sorted by a defined order. I need them to be ordered let's say as type = number first, then idk, and letter at the end.
modified my response.

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.