Page 1 of 1

Sorting alphabetically with limits.

Posted: Tue Jun 12, 2012 10:02 am
by lovelf
Hi, what I want to achieve is to sort an array alphabetically with certain restrictions,

I want to go through all of the alphabet:

Then select one name for each of the letters of the alphabet, that makes 25 selections and I need about 30 in total, so a new loop until the 30 selections get made,

I have JavaScript code but it can become very slow when there are thousands of ids to check for values.

Here's the JavasCript code:

Code: Select all

<?php

$yk=0;
asort($sortalpha);
foreach($sortalpha as $key=> $value){
$yk++;
}

$tyk=$yk-1;
?>
<?php
echo'
<script type="text/javascript">
function putavailv(h){
$("#maccnf").css("display","none");
$(".macc").css("display","none");
$(".fullnamevvv").css("display","none");
var totalr='.$tyk.';

var alphabet=new Array();

'."
alphabet[0]='a';
alphabet[1]='b';
alphabet[2]='c';
alphabet[3]='d';
alphabet[4]='e';
alphabet[5]='f';
alphabet[6]='g';
alphabet[7]='h';
alphabet[8]='i';
alphabet[9]='j';
alphabet[10]='k';
alphabet[11]='l';
alphabet[12]='m';
alphabet[13]='n';
alphabet[14]='o';
alphabet[15]='p';
alphabet[16]='q';
alphabet[17]='r';
alphabet[18]='s';
alphabet[19]='t';
alphabet[20]='u';
alphabet[21]='v';
alphabet[22]='w';
alphabet[23]='x';
alphabet[24]='y';
alphabet[25]='z';
".'

var p=0;
var o=0;
var jk=0;

while(o<=h){
if(p<21000){
var b=0;
var j=0;
var y="";
var changeletter="";
while(j<=25){

if(changeletter!="1"){
	
$("div[class=online]").each(function() {


	
                var nena=$(this).attr("id");
				y=nena.replace("bubble","");
var namemc=$("#namemc"+y).html();
namemc=strtolower(namemc);
var namemcv=namemc.split(alphabet[j]);
if(namemcv[0]==""){
var howd=$("#macc"+y).css("display");
if(howd=="none"){
$("#macc"+y).css("display","block");
$("#fullnamevvv"+y).css("display","block");
changeletter=1;
o++;
}
}				


p++;			
        }); 
		
}

changeletter=0;
		
p++;
if(p>10000){
if(o!="0"){
var now=o-1;
}
else{
var now=o;
}

var upto=h;
var s=0;

while(now!=upto)
{
var howd=$("#macc"+s).css("display");
if(howd=="none"){
$("#macc"+s).css("display","block");
$("#fullnamevvv"+s).css("display","block");
o++;
now++;
}
s++;

}


	
}
j++;
}


}
if(p>20000){o=h+1;}
}


	
}';

echo'
function putvail(){
var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName(\'body\')[0],y=w.innerHeight||e.clientHeight||g.clientHeight;
var metert=y-30;
$(".mcc").css("height",y+"px");
metert=Math.floor(metert/30)-1;

var tyk='.$tyk.';

if(metert>tyk){metert=tyk;}

putavailv(metert);

}
putavail();
</script>';
?>
So this code is doing a selection amongst div elements and is displaying a result such as:

Alexx
Ana
Gabriel
James
Peter
Roger

be it with the variable h a total of 6, it displayed the maximum 6 out of 500 for example, it found names starting with the letters: a,g,j,p,r and then it found another name again starting with an a.

The idea is not to have 6 names starting with the letter "a" which that would have been the case if I'd just stick with a regular sort.

Thanks for the help.

Re: Sorting alphabetically with limits.

Posted: Tue Jun 12, 2012 2:25 pm
by pickle
Do you want to choose the 30 in PHP, or in Javascript?

How do you choose the 5 extra? Does it loop?

Re: Sorting alphabetically with limits.

Posted: Tue Jun 12, 2012 4:04 pm
by lovelf
In PHP would be much better, for the extra 5 it currently loops.

Re: Sorting alphabetically with limits.

Posted: Tue Jun 12, 2012 4:12 pm
by pickle
I'd use 2 nested loops. An outer while() loop that continues while the number of your choices is > 30. An inner foreach or for loop would iterate through your sorted array, adding a name to your choices if it starts with a letter that is different than the last letter that was chosen. If a name is chosen, remove it from your sorted array.

On each iteration of the inner loop, check if the choices is > 30. If it is, break out of both loops.

There's probably a more elegant way to do it, this was the first I thought of.