-1

I have in my variable optionsPergunta one array options of objects that I need to sort:

I've tryed something like:

optionsPergunta.sort(this.sorteiaArray)

sorteiaArray(){
   return Math.random() < Math.random()
}

But when i do a console.log(optionsPergunta.options) the positions haven't changed

the optionsPergunta is the result of a query, so when i do a console.log(optionsPergunta) i'm having this result:

[
  BookUnitQuestionOption {
    __setters__: [
      '$attributes',
      '$persisted',
      'primaryKeyValue',
      '$originalAttributes',
      '$relations',
      '$sideLoaded',
      '$parent',
      '$frozen',
      '$visible',
      '$hidden'
    ],
    '$attributes': {
      id: 6,
      book_unit_question_id: 2,
      description: 'get',
      image_sound: null,
      correct: true,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:27.000Z,
      updated_at: 2019-12-27T19:06:27.000Z
    },
    '$persisted': true,
    '$originalAttributes': {
      id: 6,
      book_unit_question_id: 2,
      description: 'get',
      image_sound: null,
      correct: true,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:27.000Z,
      updated_at: 2019-12-27T19:06:27.000Z
    },
    '$relations': {},
    '$sideLoaded': {},
    '$parent': null,
    '$frozen': false,
    '$visible': undefined,
    '$hidden': undefined
  },
  BookUnitQuestionOption {
    __setters__: [
      '$attributes',
      '$persisted',
      'primaryKeyValue',
      '$originalAttributes',
      '$relations',
      '$sideLoaded',
      '$parent',
      '$frozen',
      '$visible',
      '$hidden'
    ],
    '$attributes': {
      id: 9,
      book_unit_question_id: 2,
      description: 'stay',
      image_sound: null,
      correct: false,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:45.000Z,
      updated_at: 2019-12-27T19:06:45.000Z
    },
    '$persisted': true,
    '$originalAttributes': {
      id: 9,
      book_unit_question_id: 2,
      description: 'stay',
      image_sound: null,
      correct: false,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:45.000Z,
      updated_at: 2019-12-27T19:06:45.000Z
    },
    '$relations': {},
    '$sideLoaded': {},
    '$parent': null,
    '$frozen': false,
    '$visible': undefined,
    '$hidden': undefined
  },
  BookUnitQuestionOption {
    __setters__: [
      '$attributes',
      '$persisted',
      'primaryKeyValue',
      '$originalAttributes',
      '$relations',
      '$sideLoaded',
      '$parent',
      '$frozen',
      '$visible',
      '$hidden'
    ],
    '$attributes': {
      id: 5,
      book_unit_question_id: 2,
      description: 'are',
      image_sound: null,
      correct: false,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:21.000Z,
      updated_at: 2019-12-27T19:06:33.000Z
    },
    '$persisted': true,
    '$originalAttributes': {
      id: 5,
      book_unit_question_id: 2,
      description: 'are',
      image_sound: null,
      correct: false,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:21.000Z,
      updated_at: 2019-12-27T19:06:33.000Z
    },
    '$relations': {},
    '$sideLoaded': {},
    '$parent': null,
    '$frozen': false,
    '$visible': undefined,
    '$hidden': undefined
  },
  BookUnitQuestionOption {
    __setters__: [
      '$attributes',
      '$persisted',
      'primaryKeyValue',
      '$originalAttributes',
      '$relations',
      '$sideLoaded',
      '$parent',
      '$frozen',
      '$visible',
      '$hidden'
    ],
    '$attributes': {
      id: 8,
      book_unit_question_id: 2,
      description: 'move',
      image_sound: null,
      correct: false,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:43.000Z,
      updated_at: 2019-12-27T19:06:43.000Z
    },
    '$persisted': true,
    '$originalAttributes': {
      id: 8,
      book_unit_question_id: 2,
      description: 'move',
      image_sound: null,
      correct: false,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:43.000Z,
      updated_at: 2019-12-27T19:06:43.000Z
    },
    '$relations': {},
    '$sideLoaded': {},
    '$parent': null,
    '$frozen': false,
    '$visible': undefined,
    '$hidden': undefined
  }
]

When i return the result, and test in postman i have this:

[
  {
    "id": 6,
    "book_unit_question_id": 2,
    "description": "get",
    "image_sound": null,
    "correct": true,
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-27 16:06:27",
    "updated_at": "2019-12-27 16:06:27"
  },
  {
    "id": 9,
    "book_unit_question_id": 2,
    "description": "stay",
    "image_sound": null,
    "correct": false,
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-27 16:06:45",
    "updated_at": "2019-12-27 16:06:45"
  },
  {
    "id": 5,
    "book_unit_question_id": 2,
    "description": "are",
    "image_sound": null,
    "correct": false,
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-27 16:06:21",
    "updated_at": "2019-12-27 16:06:33"
  },
  {
    "id": 8,
    "book_unit_question_id": 2,
    "description": "move",
    "image_sound": null,
    "correct": false,
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-27 16:06:43",
    "updated_at": "2019-12-27 16:06:43"
  }
]
veroneseComS
  • 438
  • 1
  • 8
  • 22
  • You need to sort `optionsPergunta.options`. – Barmar Dec 27 '19 at 19:23
  • @Barmar optionsPergunta is the "options" object content – veroneseComS Dec 27 '19 at 19:24
  • optionsPergunta.options is undefined – veroneseComS Dec 27 '19 at 19:24
  • 1
    what is your sorting criteria? – fubar Dec 27 '19 at 19:25
  • The quesiton shows the property of an object. What is the variable that contains the object? – Barmar Dec 27 '19 at 19:26
  • @fubar See the `sorteiaArray()` function in the question. It should sort them randomly. – Barmar Dec 27 '19 at 19:27
  • i don't have a sorting criteria, i just want to sort aleatory positions for the objects that are in optionsPergunta – veroneseComS Dec 27 '19 at 19:27
  • @Barmar you are right, i put the "options" but the content of the variable optionsPergunta is what is inside the "options" of my question – veroneseComS Dec 27 '19 at 19:29
  • It should be `const optionsPergunta = [ ... ]` without the `{ options:` part. – Barmar Dec 27 '19 at 19:32
  • @veroneseComS If your question does not match the actual data, you need to update your question to match. – Taplar Dec 27 '19 at 19:36
  • i updated my question, @Barmar the variable optionsPergunta is a result of a query: let optionsPergunta = new Array() optionsPergunta.push(optionCorreta.rows[0]), so i have the result of the edited question in the variable optionsPergunta – veroneseComS Dec 27 '19 at 19:39
  • I can't reproduce the problem: https://jsfiddle.net/barmar/x4vnLtjb/6/ – Barmar Dec 27 '19 at 19:41
  • If your question is how to sort a list of objects based on an attribute of each object (such as `id` or `created_at`) then see https://stackoverflow.com/questions/403421/how-to-sort-a-list-of-objects-based-on-an-attribute-of-the-objects – jarmod Dec 27 '19 at 19:43
  • 1
    Apparently you don't need to sort the array but to shuffle it. Shuffling it by sorting with a sorting function that returns a random value is not a good idea. For some sorting algorithms it leads to a loop that is not guaranteed to end. – axiac Dec 27 '19 at 19:44
  • sorry, i updated my question again, my object of console.log() is a bit different of the showed in postman – veroneseComS Dec 27 '19 at 19:49
  • *"But when i do a console.log(optionsPergunta.options) the positions haven't changed"* -- the comparison function (`sorteiaArray()`) is not deterministic. You cannot expect to get a certain result using it for sorting. An array having the elements in the original order is one of the many possible outcomes (and it is not incorrect). – axiac Dec 27 '19 at 19:56

2 Answers2

1

To cut a long story short. optionsPergunta refers to an object where sort() is from Array.prototype.sort() that means you need to call sort() on an array. The other thing was sorteiaArray didn't had a function in front so the interpreter though it should be a function call.

const optionsPergunta = {
    "options": [
        {
            "id": 6,
            "book_unit_question_id": 2,
            "description": "get",
            "image_sound": null,
            "correct": true,
            "status": false,
            "user_id": 1,
            "created_at": "2019-12-27 16:06:27",
            "updated_at": "2019-12-27 16:06:27"
        },
        {
            "id": 5,
            "book_unit_question_id": 2,
            "description": "are",
            "image_sound": null,
            "correct": false,
            "status": false,
            "user_id": 1,
            "created_at": "2019-12-27 16:06:21",
            "updated_at": "2019-12-27 16:06:33"
        },
        {
            "id": 7,
            "book_unit_question_id": 2,
            "description": "go to",
            "image_sound": null,
            "correct": false,
            "status": false,
            "user_id": 1,
            "created_at": "2019-12-27 16:06:39",
            "updated_at": "2019-12-27 16:06:39"
        },
        {
            "id": 8,
            "book_unit_question_id": 2,
            "description": "move",
            "image_sound": null,
            "correct": false,
            "status": false,
            "user_id": 1,
            "created_at": "2019-12-27 16:06:43",
            "updated_at": "2019-12-27 16:06:43"
        }
    ]
}

function sorteiaArray(){
   return Math.random() < Math.random()
}

console.log(optionsPergunta.options.sort(sorteiaArray))
fubar
  • 393
  • 2
  • 11
0

Apparently you don't need to sort the array but to shuffle it.

Shuffling an array by sorting it with a comparison function that returns a random value is not a good idea. For some sorting algorithms it leads to a loop that is not guaranteed to end.

If you need to shuffle the array, there is a better (and probably faster) way: pick one random item of the array, remove it from the array and append it to a new array (the shuffled one). The code could look like this:

// Create a new array to not change the original
let source = optionsPergunta.slice();
// Store the shuffled items here
let shuffled = [];

while (source.length) {
  let index = Math.floor(Math.random() * Math.floor(source.length));
  let items = source.splice(index, 1);
  shuffled.push(items[0]);
}
axiac
  • 56,918
  • 8
  • 77
  • 110