1

I have some information like this, these are not objects ..just individual data

name : 'element1', x: 30, y : 35
name : 'element2', x : 10, y:16
name : 'element3', x: 70, y:11

i have pushed all three infos to three different arrays

var elmArray = ['element1','element2','element3'];
var xArray = [30, 10, 70];    
var yArray = [35, 16, 11];

sorted the xArray and yArray in asc order like this

xArray.sort(function(a, b){return a - b});
yArray.sort(function(a, b){return a - b});

My question is how do I sort the elmArray with respect to xArray and yArray for example after xArray sort I want elmArray like this

['element2', 'element1', 'element3'];

and after yArray I want it like this

['element3', 'element2', 'element1'];
4
  • 2
    Why are you sorting all the keys individually? Why not sort the array of objects? Commented Jul 16, 2015 at 11:21
  • 5
    "I have some information like this ,these are not objects ..just individual data" - if the data is related then they should be objects! Related could mean that you want to sort one in respect of the other. Commented Jul 16, 2015 at 11:25
  • 2
    This is a duplicate of countless questions. Commented Jul 16, 2015 at 11:26
  • have a look at this, github.com/Teun/thenBy.js Commented Jul 16, 2015 at 11:30

3 Answers 3

3

To start with, you must make your data into an array of objects.

var arr = [
    {name : 'element1' ,x: 30, y : 35},
    {name : 'element2', x : 10, y:16},
    {name :'element3', x: 70, y:11}];

Then you know how to sort an array!

arr.sort(function(a,b){ return a.x - b.x; } ); // sort by x

Once sorted, if you want an array containing just one property you can use .map

var names = arr.map(function(e) { return e.name; });
Sign up to request clarification or add additional context in comments.

Comments

3

Sort them as objects:

var array = [
    {name : 'element1' ,x: 30, y:35 },
    {name : 'element2', x: 10, y:16 },
    {name : 'element3', x: 70, y:11 }
]

var sorted = array.sort(function (prev, next) { return prev.x - next.x });

if you want individual portions of data:

var elements = sorted.map(function (e) { return e.name });

1 Comment

Well, it actually does, here is citation from MDN - The sort() method sorts the elements of an array in place and returns the array.
3

var elmarray = ['element1','element2','element3'];
var xarray = [30,10,70];    
var yarray = [35,16,11];
finalArray = sort(xarray, elmarray);
xarray = finalArray.first;
elmarray = finalArray.second;
console.log(xarray);
console.log(elmarray);

function sort(a, b){
   for (var index = 0; index < a.length-1; index += 1) {
            nextIndex = index + 1;
            if (a[index] > a[nextIndex]) {
                holder1 = a[nextIndex];
                holder2 = b[nextIndex];
                
                a[nextIndex] = a[index];
                b[nextIndex] = b[index];
                
                a[index] = holder1;
                b[index] = holder2;
              }
        }
    return { first: a,
            second: b}
}

also see http://jsfiddle.net/et5zL3ve/

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.