1

I have a bit of a challenge. I am working on a physics application with javascript. The two main objects being used are

var force = new Object();
var torque = new Object();
with properties
force.magnitude = newArray();
force.lengthfromorigin = new Array();
force.count;
torque.lengthfromorigin= new Array();
torque.count;

now, I'd like to sort these two objects into an array based on their respective lengthfromorigins

Example: force.lengthfromorigin = [5,8] and torque.lengthfromorigin=[2,6] so their order in this newArray would be [ torque[0], force[0], torque[1], force[1] ]

My question is it possible to have an array of different objects sorted by their respective properties, and to then use this array in a function which will make decisions based on which object is at the index. Also will I need to have an id property in each respective object to identify if the object is a torque or force.

Example:

if(newArray[i] == torque)
    //do stuff
else
    //do other stuff.
3
  • 1) likely, 2) please use your first example in the next example Commented Oct 19, 2013 at 4:43
  • I think you're trying to solve too much at once. I'd start by coding a sorting method for an array of integers, then work your way up from there. Commented Oct 19, 2013 at 4:44
  • its pseudo code @PsychHalf ... Commented Oct 19, 2013 at 4:56

2 Answers 2

2

Something like this perhaps?

Let me explain the algorithm:

  1. Create a new array let it be called A.
  2. For each objects in objects:

    2.1 Let the current object be called obj.

    2.2 Use map to generate a new array called tuples of [obj, num] tuples for each lengthFromOrigin numbers of obj.

    3.3 Push all items of tuples into A.

  3. Sort A on tuple[1] (which is the number) ascending.

var objects = [
        { type: 'force', lengthFromOrigin: [5, 8] },
        { type: 'torque', lengthFromOrigin: [2, 6] }
    ],
    sorted = objects.reduce(function (arr, obj) {
        arr.push.apply(arr, obj.lengthFromOrigin.map(function (num) {
            return [obj, num];
        }));

        return arr;           
    }, []).sort(function (a, b) {
        return a[1] - b[1];    
    });

console.log(sorted);

Then you can loop over sorted and easily identify if it's a torque or force by looking at the first element in the tuple.

sorted.forEach(function (tuple) {
    console.log(tuple[0].type, tuple[1]);
});

//torque 2
//force 5
//torque 6
//force 8 
Sign up to request clarification or add additional context in comments.

1 Comment

@bala, You can obviously encapsulate the algorithm in a function if you plan to do this often ;)
0

The answer is Yes,

But you have to identify each object before you access their properties. In your case Both of the objects have a common property called lengthfromorigin which can be used to sort them properly.

To identify each object you can use a property like ID or Name.

if(Mydata[i].Name = 'torque'){
  //your code goes here
}
else if(Mydata[i].Name = 'force'){
  //your code goes here
}

Hope this will help you

1 Comment

Why not just use object oriented principles and put a commonly named method on each object type that can execute the desired code for that type of object so you don't have to look at what type it is?

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.