1

I have a created 4 dimensional array in JavaScript. One column contains String values and the other three columns contain float values. I filled the first 3 columns from a JSON response and the fourth column contains the magnitude of the values col2 and col3. I need to sort the entire array based on the values in col4. Following code shows how I created the array. Can someone please tell me how to sort this array based on col4?

     var labels = responseJSON.labels;
     var knowledge = responseJSON.knowledge;
     var interest = responseJSON.interest;

     var label_know_int_array = new Array(3);
     //array item 0 contains all the labels
     label_know_int_array[0] = new Array(200);
     //array item 1 contains knowledge values
     label_know_int_array[1] = new Array(200);
     //array item 2 contains all the interest values
     label_know_int_array[2] = new Array(200);
    //array item 3 contains the magnitude of knowledge and interest
     label_know_int_array[3] = new Array(200);

     for(var i=0;i<labels.length;i++)
         {
                label_know_int_array[0][i] = labels[i];
                var knowVal = knowledge[i];
                var intVal = interest[i];
                label_know_int_array[1][i] = knowVal;
                label_know_int_array[2][i] = intVal ;

                var squareSum = (knowVal*knowVal) + (intVal*intVal);
                var distance = Math.sqrt(squareSum);
                label_know_int_array[3][i] = distance ;


         }
5
  • 1
    This is a 2-dimensional array and you need to re-order your dimensions Commented Mar 14, 2012 at 8:04
  • @kirilloid - Isn't this a 4 dimensional array ? I do not understand your comment well. What do you mean I have to re-order the dimensions ? Commented Mar 14, 2012 at 8:09
  • This is a 2D-array: a[][]. 4D looks like: a[][][][]. You have to switch label_know_int_array[3] to label_know_int_array[0] to sort by distance. Commented Mar 14, 2012 at 8:15
  • 1
    @Kumaripaba I meant using 200x4 array instead of 4x200 like in Nemoy's answer. I.e. group by rows, not columns. Commented Mar 14, 2012 at 8:27
  • @kirilloid-Thanks alot.I have had a little confusion with the array structure. I followed ur advice and created a 4x200 array and sorted following Nemoy's answer. Thanx again Commented Mar 14, 2012 at 9:34

1 Answer 1

1

You can use native Array.sort() method

Array.sort method accepts a callback for custom sorting

here is a sample code

var arr = [
  {
    id : 5,
    name : "john"
  },
  {
    id : 2,
    name : "Sam"
  },
  {
    id : 6,
    name : "adams"
  }
];

// custom sort
 arr.sort(function (a, b) {
    if (a.id > b.id) {
       return 1;   //return any +ve values
    } else if (a.id < b.id) {
       return -1;  // return any -ve values
    } else {
       return 0;  // return zero for equal values
    }
 });

Using this callback you can implement your custom sorting functionality

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.