0

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.

10
  • How do you want to sort it? Based on what? Commented Sep 24, 2017 at 11:48
  • What is the context in which you want to sort the elements? As the question is tagged with angularjs you could look into orderBy Commented Sep 24, 2017 at 11:49
  • @ulferts yes am l used orderby but it not's work.here i have to sort based on standard id ,can you please look in to the expected result. Commented Sep 24, 2017 at 11:56
  • @mrgeek based on standardID Commented Sep 24, 2017 at 11:58
  • 2
    How is 1 3 2 2 2 201 grouped? I don't see a logic behind it! Commented Sep 24, 2017 at 12:39

4 Answers 4

1

You can try this prototype custom sorting by attributes objects :

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

https://codepen.io/anon/pen/KXNpMZ?editors=1112

@Sathish

UPDATED in ordered by indexes array reference here :

https://codepen.io/anon/pen/RLoZOX?editors=1112

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

2 Comments

am not looking for ascending order.i have to grouping like 1,3,2,2,2,201 as well
@Sathish look at the second link about sorting by array indexes reference, regards.
0

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

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

Fiddle:

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

7 Comments

:here am not looking for ascending order ... based on the standard id only i need to grouping array of objects.
ok, now it's using standardID, but can you update your question with real expected result?
expected result [{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"} } ],here order like first 1,second 3,third 2,fourth 2,fifth 2,sixth 201
@pegala why dont it works with following code change kindly reply me:var groupOrder = [1,3,2,201]; var testSorted = test.sort(function (a, b) {groupOrder.indexOf(parseInt(a.standardID))-groupOrder.indexOf(parseInt(b.standardID))});
I updated the answer, it's all about that groupingOrder - if you have 1,3,2,201 it will sort them in that order - I just noticed you wanted 201 before 2 so I just changed it to 1,3,201,2
|
0

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


// parseInt used because of your input datatype
var sortOrder = [1,3,2,201];
var out = test.sort(function(a, b) {
    return sortOrder.indexOf(parseInt(a.standardID)) - sortOrder.indexOf(parseInt(b.standardID));
});

//sorted
console.log(out)

Comments

0

You can use JavaScript's sort function with your own criteria (map):

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"}
];
var myMap = {
            "1": 0,
            "3": 1,
            "2": 2
          };

test.sort((a, b) => myMap[a.standardID[0]] - myMap[b.standardID[0]]);
console.log(test);

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.