简体   繁体   English

对数组内的对象进行排序

[英]Sort objects inside a array

I have in my variable optionsPergunta one array options of objects that I need to sort:我的变量optionsPergunta一个我需要排序的对象数组options

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但是当我执行console.log(optionsPergunta.options) ,位置没有改变

the optionsPergunta is the result of a query, so when i do a console.log(optionsPergunta) i'm having this result: optionsPergunta 是查询的结果,所以当我做一个 console.log(optionsPergunta) 我有这个结果:

[
  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"
  }
]

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. optionsPergunta指的是sort()来自Array.prototype.sort()的对象,这意味着您需要对数组调用sort() The other thing was sorteiaArray didn't had a function in front so the interpreter though it should be a function call.另一件事是sorteiaArray没有function ,因此解释器虽然应该是函数调用。

 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))

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]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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