1

I'm learning javascript and can't figure out the problem in my code below. I guess I misunderstand how arrays actually work.

The problem is that the alerted array is not actually sorted. I would be grateful for any insights on this:

Here is a ready jsFiddle.

HTML:

<html>
  <head>
    <title>
      Test 1
    </title>
    <script src="http://code.jquery.com/jquery-latest.js" type=
    "text/javascript">
</script>
  </head>
  <body>
    <div class="items">
      <ul>
        <li>
          <a href="#" id="1">Link 1</a>
        </li>
        <li>
          <a href="#" id="2">Link 2</a>
        </li>
        <li>
          <a href="#" id="3">Link 3</a>
        </li>
        <li>
          <a href="#" id="4">Link 4</a>
        </li>
      </ul>
    </div>
</body>
</html>

Javascript:

var clickedLinks = [];
var passedItems = [];


// Collect clicked link IDs into an array and pass the array as an argument to shoplist()
$('.items a').click(function () {

if (clickedLinks.indexOf(this.id) != -1) {
    var linkIndex = clickedLinks.indexOf(this.id);
    clickedLinks.splice(linkIndex, 1);
} else {
    clickedLinks.push(this.id);
}

shoplist(clickedLinks);

});


function shoplist(ids) {
    passedItems.push(ids.slice());
    alert(passedItems.sort());
}

Steps to reproduce:

  1. Click Link 2
  2. Click Link 1

Expected Result: 1, 2, 2

Actual Result: 2, 2, 1

Things I've tried:

function shoplist(ids) {
    passedItems.push(ids.slice());
    var newpi = passedItems.slice();
    alert(newpi.sort());
    }
1
  • You have an array of arrays. Try not pushing to passed items but just sort clickedlinks Commented Aug 26, 2012 at 19:08

3 Answers 3

2

I think you're using the sort function wrong. If you want to use the sort function numerically, you have to include a sort function. If you want to sort things numerically you should use this function (provided in the link):

(function(a,b){return a-b});

So in your scenario for the newpi it would look like this:

newpi.sort(function(a,b){return a-b});
Sign up to request clarification or add additional context in comments.

Comments

1

You are adding an array, the result of ids.slice(), to another array. That's why the sort doesn't work as you expect. Use concat instead, if you want to add the contents of ids to the passedItems array:

passedItems = passedItems.concat(ids);

Also, instead of adding a string to the ids array, you should use .push(parseInt(this.id)) instead, and then use:

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

1 Comment

If I'm not mistaken, sort() method will always sort alphabetically whether you are passing integers or strings. To sort numerically you'll need to pass the sorting function as an argument, see stackoverflow.com/a/12133066/932210
0

I hope this will work:

http://jsfiddle.net/DZpDG/4/

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.