Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

randomize a list of search results

I'm trying to randomize a list of search results returned from a query using APEX on the Salesforce platform. The closest language to APEX is Java so I added this to the Java topic in hopes a java expert could help.  Right now, the same results are returned in the same order when using a specific search algorithm. It returns the list in the same order every time and the user sees the first series of records in the UI. I want to randomize that list so the first series of records will be different each time the user searches using the same search algorithm.

I created a method to return a random integer based on the lower and upper bounds of the search results list:

public static Integer getRandomInt (Integer lower, Integer upper)
{
	return (Integer) (Math.floor(Math.random() * (upper - lower)) + lower);
}

Open in new window


I'm stuck on how I can set each element in the list with the random integer as the index until all elements have been set and I don't want any indexes to repeat.  So, if I return 100 results back, as I iterate of the list, a random integer is generated, the index of that list element is set with that integer value within the upper and lower bounds, then the next iteration occurs, checks if the next integer has not already been used and then sets the index for the next list element. This continues until all list elements have been set. If I'm thinking the logic thru correctly, I should have a completely random list of search results.  

How can I accomplish this using the Salesforce APEX language?  Is there a better way to do this? Salesforce does not have a method to randomize a list or keyword to return a soql query in a random order, so the only way I can think to do this is using the math functions and create a new list and randomly setting each element index.

Any help is greatly appreciated.
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you please tell me why you marked an offer of help as an accepted answer rather than an actually helpful answer?
Avatar of -Dman100-

ASKER

Well, I wanted to split the points to both people that responded to my post. I did a lot of research into options to randomize results in APEX and there are some fundamental limitations that is not an issue in Java. Since you both offered help, I wanted to extend points to both of you. I certainly did not think this would be an issue?