繁体   English   中英

如何在 apex salesforce 中随机排列列表

[英]How to shuffle a list in apex salesforce

我想对列表记录进行随机播放,我正在创建一个 VF 页面来显示有关该问题的问题。 当页面重新加载时,每次都希望在页面上随机出现另一个问题。 我在用

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

在开发者控制台中它运行良好,但在 vf 页面上它不起作用 问题未显示或有限 问题显示

任何人都建议我在 vf 页面上显示问题的更好方法

提前致谢

我构建了一个用户使用 Fishe-Yates shuffle 的解决方案:

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

暂无
暂无

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

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