0

Following solution will be worked well when am using lambda function.due to some karma test case failure i have to avoid lambda function in sorting.i don't know how to overcome this issue.now the following code is not working for me

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 groupOrder = [1,3,2,201];
var testSorted = test.sort(function (a, b) {groupOrder.indexOf(parseInt(a.standardID))-groupOrder.indexOf(parseInt(b.standardID))}); 


console.log(testSorted);

The same concept will be working on the following link: fiddle working link

And following link is my actual question: How do we customize the grouping from actual array of objects in angularjs

3
  • 1
    "due to some karma test case failure" – I don't see how that's connected to a sort callback. You should ask a concrete question about that issue. xyproblem.info Commented Sep 26, 2017 at 7:45
  • Shouldn't it be var testSorted = test.sort((a, b) => parseInt(a.standardID)-parseInt(b.standardID));? Commented Sep 26, 2017 at 7:45
  • 1
    "due to some karma test case failure i have to avoid lambda function in sorting". This sounds like an XY Problem. Instead of asking how to solve your problem (karma test failure), you are asking about your potential solution (avoiding lambda), which may or may not be correct or useful. You might consider actually asking about the karma error, instead. Commented Sep 26, 2017 at 7:46

1 Answer 1

2

You need to return the result of sort calculation.

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" }],
    groupOrder = [1, 3, 2, 201];

test.sort(function(a, b) {
    return groupOrder.indexOf(+a.standardID) - groupOrder.indexOf(+b.standardID);
    // ^^^
});

console.log(test);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

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.