Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
Javascript sorting array quiz
const questions = [
  {
    questionText: "What is the capital of France?",
    answerOptions: [
      { answerText: "New York", isCorrect: false },
      { answerText: "London", isCorrect: false },
      { answerText: "Paris", isCorrect: true },
      { answerText: "Dublin", isCorrect: false }
    ].sort(() => Math.random() - 0.5), //it sorts but when clicked it generate new random values(position change which is not good) in answerOptions1
    answerOptions1: [
      { answerText: "New York1", isCorrect: false },
      { answerText: "London1", isCorrect: false },
      { answerText: "Paris1", isCorrect: true },
      { answerText: "Dublin1", isCorrect: false }
    ].sort(() => Math.random() - 0.5) //it sorts but when clicked it generate new random values in answerOptions
  },
{},
{}
]
.sort(() => Math.random() - 0.5) // this sorting is not working
; 

codesanbox link: https://codesandbox.io/s/lucid-zeh-5ulr5z?file=/src/App.js

I need to sort questions, answerOptions and answerOptions1 array in random order. The .sort(() => Math.random() - 0.5) is not working for this logic. When answerOptions is clicked, it also generate new random position in answeOptions1. answerOptions and answerOptions1 should not change their position and data/value(when fetch from large list).

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your randomly generated array will change in each render. use React useMemo hook to generate random array once.

const questions = useMemo(()=>[
  {
    questionText: "What is the capital of France?",
    answerOptions: useMemo(()=>[
      { answerText: "New York", isCorrect: false },
      { answerText: "London", isCorrect: false },
      { answerText: "Paris", isCorrect: true },
      { answerText: "Dublin", isCorrect: false }
    ].sort(() => Math.random() - 0.5), []), //it sorts but when clicked it generate new random values(position change which is not good) in answerOptions1
    answerOptions1: useMemo(()=>[
      { answerText: "New York1", isCorrect: false },
      { answerText: "London1", isCorrect: false },
      { answerText: "Paris1", isCorrect: true },
      { answerText: "Dublin1", isCorrect: false }
    ].sort(() => Math.random() - 0.5), []) //it sorts but when clicked it generate new random values in answerOptions
  },
{},
{}
].sort(() => Math.random() - 0.5), [])
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!