如何在apex salesforce中洗牌列表
我想洗牌一个列表记录,我创建一个VF页面来显示这个问题。当重新加载页面时,每次都希望页面上随机出现另一个问题。 我正在使用如何在apex salesforce中洗牌列表,salesforce,visualforce,apex,Salesforce,Visualforce,Apex,我想洗牌一个列表记录,我创建一个VF页面来显示这个问题。当重新加载页面时,每次都希望页面上随机出现另一个问题。 我正在使用 Integer count = [SELECT COUNT() FROM Question__c ]; Integer rand = Math.floor(Math.random() * count).intValue(); List<Question__c > randomQuestion = [SELECT Id, Question__c
Integer count = [SELECT COUNT() FROM Question__c ];
Integer rand = Math.floor(Math.random() * count).intValue();
List<Question__c > randomQuestion = [SELECT Id, Question__c
FROM Question__c
LIMIT 1000 OFFSET :rand];
system.debug('<<<<<<<<<>>>>>>>>>'+randomQuestion);
Integer count=[从问题中选择count()];
整数rand=Math.floor(Math.random()*count.intValue();
List randomQuestion=[选择Id,问题\uu\c
来自问题c
限制1000偏移量:兰特];
系统调试(“”+随机问题);
在开发人员控制台中运行良好,但在vf页面上不工作
不显示问题或限制显示问题
有人建议我用更好的方式在vf页面上显示问题吗
提前感谢我构建了一个解决方案,让Fishe-Yates用户洗牌:
list<Question__c> quesLst = [select id, Question__c, RecordType.Name, Answer__c, Option_A__c, Option_B__c, Option_C__c, Option_D__c from
Question__c limit 1000];
randomize(quesLst);
private list<Question__c> randomize(list<Question__c> lst){
integer currentIndex = lst.size();
Question__c Question;
integer randomIndex;
// While there remain elements to shuffle...
while (0 != currentIndex) {
// Pick a remaining element...
randomIndex = integer.valueOf(Math.floor(Math.random() * currentIndex));
currentIndex -= 1;
// And swap it with the current element.
Question = lst[currentIndex];
lst[currentIndex] = lst[randomIndex];
lst[randomIndex] = Question;
}
return lst;
}
list quesLst=[从中选择id、问题、记录类型、名称、答案、选项A、选项B、选项c、选项D
问题uu_c限制1000];
随机化(提问);
私有列表随机化(列表lst){
整数currentIndex=lst.size();
问题(c)问题;;
整数随机指数;
//虽然还有一些元素需要洗牌。。。
而(0!=当前索引){
//选择剩余的元素。。。
randomIndex=integer.valueOf(Math.floor(Math.random()*currentIndex));
currentIndex-=1;
//并将其与当前元素交换。
问题=lst[当前索引];
lst[currentIndex]=lst[randomIndex];
lst[随机指数]=问题;
}
返回lst;
}
您是否需要以随机方式显示整个列表?我想随机回答您想显示多少个问题?我有3个部分(a、b和c部分)我每个部分只回答10个问题。每次加载页面时,我都想从数据库中获取另一个问题。您检查过了吗