我必须使用jquery对3个数组进行排序,但结果的顺序应相同
第一个数组用于文本答案第二个用于图像应答,第三个用于音频应答排序后所有数组应该是相同的顺序,但是当问题改变时它必须排序
var Textanswers = plugin.config.randomSort || plugin.config.randomSortAnswers ?
question.a.sort(function () { return (Math.round(Math.random()) - 0.5); }) :
question.a;
var Imageanswer= plugin.config.randomSort || plugin.config.randomSortAnswers ?
question.imga.sort(function () { return (Math.round(Math.random()) - 0.5); }) :
question.imga;
var Audioanswer= plugin.config.randomSort || plugin.config.randomSortAnswers ?
question.auda.sort(function () { return (Math.round(Math.random()) - 0.5); }) :
question.auda;
答案 0 :(得分:0)
执行此操作的一种方法是使用索引数组,然后将其改为洗牌。
然后,您可以使用混洗数组中的索引以相同的随机顺序访问每个其他数组的元素。
以下C#代码演示了该方法(以及标准的shuffle算法):
using System;
using System.Collections.Generic;
using System.Linq;
namespace Demo
{
class Program
{
static void Main()
{
int[] array1 = {1, 2, 3, 4, 5};
string[] array2 = {"One", "Two", "Three", "Four", "Five"};
double[] array3 = {0.1, 0.2, 0.3, 0.4, 0.5};
// Create an array of indices the same length as the arrays to be shuffled.
var indices = Enumerable.Range(0, array1.Length).ToArray();
// Shuffle the indices.
Shuffle(indices, new Random());
// Now you can use the shuffled indices to access all the arrays in the same random order:
foreach (int index in indices)
{
Console.WriteLine($"{array1[index]}, {array2[index]}, {array3[index]}");
}
}
// Standard shuffle.
public static void Shuffle<T>(IList<T> array, Random rng)
{
for (int n = array.Count; n > 1;)
{
int k = rng.Next(n);
--n;
T temp = array[n];
array[n] = array[k];
array[k] = temp;
}
}
}
}
答案 1 :(得分:0)
有many ways来重新排列数组,但我会在这个答案中使用你的:
const shuffle = xs => xs.sort(() => Math.round(Math.random()) - 0.5));
您的问题包含3个类似长度的数组。数组中的项目通过索引相互“相关”:
question.a[i] ~ question.imga[i] ~ question.auda[i]
确保这些关系在改组时得到尊重的一种方法是将“三重”项目而不是单个数组洗牌。
shuffle([
[question.a[0], question.imga[0], question.auda[0]],
/* ... */
])
这留下了两个挑战: 1.从多个阵列创建一个相关项的数组 2.排序后,从混洗三元组中提取单个数组
让我们定义一个zip函数:
const zip = (xs, ...others) =>
xs.map(
(x, i) => [x].concat(others.map(ys => ys[i]))
);
console.log(zip([1, 2, 3], ["a", "b", "c"], ["I", "II", "III"]));
现在,为了回到三个独立的数组,我们创建了一个“问题构造函数”,它接受[a, imga, auda]
数组并将其转换为具有命名属性的对象:
const Question = ([a, imga, auda]) => ({ a, imga, auda })
或更通用:
const zipped = [[1, "a", "I"], [2, "b", "II"], [3, "c", "III"]];
const unzip = xss => xss.reduce(
(t1, t2) => t1.map((x, i) => [].concat(x).concat(t2[i]))
);
console.log(unzip(zipped));
const q = {
question: "Are my answers shuffled?",
answers: [ "Yes", "No", "Maybe" ],
audio: [ "yeeeeaaah", "naaaa", "ehhhh" ],
img: [ "✔", "⛔️", "❓" ]
};
// Utils
const zip = (xs, ...others) =>
xs.map(
(x, i) => [x].concat(others.map(ys => ys[i]))
);
const unzip = xss => xss.reduce(
(t1, t2) => t1.map((x, i) => [].concat(x).concat(t2[i]))
);
const shuffle = xs => xs.sort(() => Math.random() - 0.5)
// Question shuffler implementation
const shuffleQuestion = q => {
const [answers, audio, img] =
unzip(shuffle(zip(q.answers, q.audio, q.img)));
return Object.assign(
{}, q, { answers, audio, img }
);
};
// Three versions of the question that *might* have
// differently ordered answers
console.log(shuffleQuestion(q));
console.log(shuffleQuestion(q));
console.log(shuffleQuestion(q));
当然,您还可以创建Matthew提供的方法的javascript实现:
const reorder = order => xs => order.map(i => xs[i]);
const range = length => Array.from({ length }, (_, i) => i);
const shuffle = xs => xs.sort(() => Math.random() - 0.5);
const myShuffle = reorder(shuffle(range(3)));
console.log(myShuffle([1, 2, 3]));
console.log(myShuffle(["a", "b", "c"]));
console.log(myShuffle(["I", "II", "III"]));