Sathish Sathish - 25 days ago 9
AngularJS Question

How do we customize the grouping from actual array of objects in angularjs

I have to create my customized grouping order based on

standardID
in the following array of objects:

Actual Array:

var test=[
{ID: "91",Name: "sgtue", standardID: "1"},
{ID: "41",Name: "asdfasdf", standardID: "2"},
{ID: "5", Name: "credd", standardID: "2"},
{ID: "2",Name: "dddawer", standardID: "2"},
{ID: "2",Name: "dsfadf", standardID: "3"},
{ID: "275", Name: "xcvcvc", standardID: "201"}
]


Expected Result: I Simply want to group the
standardID
's like
1,3,2,2,2,201


[
{ID: "91",Name: "sgtue", standardID: "1"},
{ID: "2",Name: "dsfadf", standardID: "3"},
{ID: "275", Name: "xcvcvc", standardID: "201"},
{ID: "41",Name: "asdfasdf", standardID: "2"},
{ID: "5", Name: "credd", standardID: "2"},
{ID: "2",Name: "dddawer", standardID: "2"}
]


Kindly help me to sort out the problem.

Answer Source

It's pure javascript and this will "sort" it your way, define your sort order in groupOrder variable:

var groupOrder = [1,3,2,201];
var testSorted = test.sort((a, b) => groupOrder.indexOf(parseInt(a.standardID))-groupOrder.indexOf(parseInt(b.standardID))); 

Fiddle:

https://jsfiddle.net/pLe44x1x/1/