简体   繁体   中英

How do I sort and shuffle javascript array?

I'm working on a drag and drop card game . I'm able to use sort and use Math.Random to shuffle:

answers.sort( function() { return Math.random() - .5 } );

It works but after the cards are placed in the correct subcontainer the wrong year appears below as if it's shuffling.

Is it possible to shuffle an array by the image but still be able to sort by value(order)?

  var answers = [
    {
      "image":'https://www.floridamemory.com/onlineclassroom/game/cards/1822.png',
      "order":'1'
    },{
      "image":'https://www.floridamemory.com/onlineclassroom/game/cards/1839.png',
      "order":'2'
    },
    {
      "image":'https://www.floridamemory.com/onlineclassroom/game/cards/1912.png',
      "order":'3'
    }
  ];

Solved it by following Order two Arrays based on the same random sorting system

Just added an extra array to help the shuffle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM