Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a sorting problem in javascript, I need to sort an array and sort the captions (saved in another array) in the same order (descending), how can I sort them the same way?

For the clarity of my post I will reduce it to a basic example:

var arr = Array(9, 5, 11, 2, 3);
var arrCaptions = Array("some text","another bit of text","three", "four?", "maybe five?");

Now I want to run a kind of sort mechanism that sorts the arrCaptions array the same way as the arr array, so as result you would get this:

var arrResult = Array(11, 9, 5, 3, 2);
var arrCaptionsResult = Array("three", "some text" ,"another bit of text", "maybe five?", "four?");

What I have tried so far doesn't work at all:

var numlist = Array(9, 5, 11, 2, 3);
var list = Array("four?","maybe five?","another bit of text","some text","three");

var resultnumlist = Array();
var resultlist = Array();

resultnumlist[0] = numlist[0];
resultlist[0] = list[0];

for (i = 0; i < list.length; i++) {
     var i2 = list.length - 1;
     while (numlist[i] < resultnumlist[i2]) {
        i2--;
     }
     resultnumlist.splice(i2 - 1,0,numlist[i]);
     resultlist.splice(i2 - 1,0,list[i]);
}
share|improve this question
1  
Although, you have selected your answer, If you could explain the logic you have tried building, I can help fix your code. Are you just trying to find the lowest number in each run and then insert it into your result array? If so, check:jsbin.com/awaveb/3/watch –  closure Dec 17 '12 at 16:25
    
Yes, that's what I tried, I checked your code and that's actually what I wanted to percieve. :) Just dit it a wrong way, can't find my own mistakes though... –  Rsauxil Dec 17 '12 at 16:38
    
My logic was the following: Start with the last element (that will be the lowest if the algorithm works well), loop through the results array until the value of the current is bigger than the result array index and then remember the number of that index. Then I would insert the value of the new element at that index, just as I would do with the caption of the value. The result would be a perfectly sorted array, in theory :P –  Rsauxil Dec 17 '12 at 16:57
1  
Got it. Let me code that! –  closure Dec 17 '12 at 17:00

3 Answers 3

up vote 1 down vote accepted

Here is your modified code:

  var numlist = Array(9, 5, 11, 2, 3);
  var list = Array("nine?","maybe five?","another bit of 11","some 2","three");

  var resultnumlist = new Array();
  var resultlist = new Array();

  for (i = 0; i < list.length; i++) {
       var i2 = resultnumlist.length - 1;
       while ((numlist[i] < resultnumlist[i2]) && (i2 >= 0)) {
          i2--;
       }
       i2++;
       resultnumlist.splice(i2, 0, numlist[i]);
       resultlist.splice(i2, 0, list[i]);
  }
  console.log(resultlist);
  console.log(resultnumlist);

See it working

share|improve this answer
    
@user1493235 here is your code edited following your logic. –  closure Dec 17 '12 at 17:21
    
My error was in the i2++ and the while loop, I get my mistake now if I think it out logically with an example. So now I don't only got a solution, I learned something new and know my fault, and that's more important to me :) Thank you very much! –  Rsauxil Dec 17 '12 at 17:26

Bundle them together in an object.

var stuff = [{
    id: 9,
    text: "text hello"
}, {
    id: 5,
    text: "text world"
}, {
    id: 11,
    text: "text test"
}, {
    id: 2,
    text: "text 23"
}];

stuff.sort( function( a, b ) {
     return a.id - b.id; //Objects are sorted ascending, by id.
});

The result is:

[{
    "id": 2,
    "text": "text 23"
}, {
    "id": 5,
    "text": "text world"
}, {
    "id": 9,
    "text": "text hello"
}, {
    "id": 11,
    "text": "text test"
}]
share|improve this answer

How about combining them into one array? Then you can sort this array based on the value of the numbers, and the captions get sorted in tandem:

//Your arrays
var arr = [9, 5, 11, 2, 3];
var arrCaptions = ["some text", "another bit of text", "three", "four?", "maybe five?"];

//The composite array
var composite = arr.map(function(v, i) {
    return {
        rank: v,
        caption: arrCaptions[i]
    };
});

//Sort this array
composite.sort(function(a, b) {
    return a.rank - b.rank;
});

console.log(composite);

Here is a demonstration: http://jsfiddle.net/cFDww/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.