开发者

Ordering a list randomly

开发者 https://www.devze.com 2023-01-11 18:35 出处:网络
I have a list of 55 (or any number but 55 at the moment) questions in my iPhone app. I have written it at the moment so it goes through the questions from 1 to 55 in number order.

I have a list of 55 (or any number but 55 at the moment) questions in my iPhone app.

I have written it at the moment so it goes through the questions from 1 to 55 in number order.

However, I would like to make this order random (or pseudo-random anyway).

I can do it programatically by generating a random number to pick one of the questions and then creating a second list of numbers and c开发者_运维知识库hecking each time that I haven't already got it before putting that question in the list and picking a new random question.

I would like to know if there is a better/easier way of doing this though?

Like it's possible to sort lists by numerical or alphabetical order using functions, is it possible to sort them randomly and what type of list should I use?

Any help is appreciated.

Thanks

Oliver


Fisher–Yates shuffle


Use a NSMutableArray, send it a sortUsingSelector: message. In the selector, return NSOrderedAscending and NSOrderedDescending randomly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号