Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select List Sort Employee Banding

Status
Not open for further replies.

seanybravo

IS-IT--Management
Sep 24, 2003
89
GB
I have Employee banding in a Select list that is part of a pick list i.e. 1-10, 11-25 etc. But I am having a nightmare on sorting it. I initially load the select list in order but when things are moved about in the select list the order gets changed. Here is the code I have been working on. I am new to sorting so I am struggling a bit.

Any help would be appreciated.

Thanks

Sean

Code:
function compareOptionNumber(a,b) { 
  /* 
   * return >0 if a>b 
   *         0 if a=b 
   *        <0 if a<b 
   */ 
  // textual comparison 
  //return a.text!=b.text ? a.text<b.text ? -1 : 1 : 0; 
  // numerical comparison 
	
  return a.text - b.text
}

function sortOptionsN(list) { 
//alert("employees")

  var items = list.options.length; 
  // create array and make copies of options in list 
  var tmpArray = new Array(items); 
  for ( i=0; i<items; i++ ) 
    tmpArray[i] = new 
Option(list.options[i].text,list.options[i].value); 
  // sort options using given function 
  tmpArray.sort(compareOptionNumber); 
  // make copies of sorted options back to list 
  for ( i=0; i<items; i++ ) 
    list.options[i] = new Option(tmpArray[i].text,tmpArray[i].value); 
}

Here is an example of the employee banding

1-9
10-24
25-49
50-99
100-199
200+
 
i don't think you can use a sort function to sort an array of Objects. you'll probably need to write a custom script to compare each option's text and/or value and insert the option into the specific index of an array.



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Set up a loop to read the text and value of each option into a new array, sort that array and then clear and re-populate the select box.

However, since you are using numbers for the text of the select you might run into problems getting the sort to work the way you want it to.
A typical alphabetic sort would come out:
100-199
10-24
1-9
200+
25-49

If you make the value sequential text you can sort by that.


Google, you're my hero!
 
Ok I am nearly at a solution I am trying just to extract the From aspect of the employee information and then using that as my sort key, however I am having problems with getting just the first part out. Here is my code:

Code:
function sortEmployee(list){
	var listLength = list.options.length
	var tempArray = new Array(listLength)
	var str
	
	for(i=0; i <= listLength;i++){
		//Extract the from number for sorting
		str = list.options[i].text
		//alert(list.options[i].text)
		alert('The List Length is: ' + listLength + ' at list no ' + i)
		
		j = 1
		//alert("SW" + list.options[i].text.slice(0,j))
		while (IsNumeric(str.charAt(j)) != false){
			alert(str.slice(0,j))
			j++
			//alert('WHILE COUNT '+j)
		}		
		
		//tempArray[i] = new objEmployee(new Option(list.options[i].text,list.options[i].value),list.options[i].
		}

}
	
function IsNumeric(sText) { 
   var ValidChars = "0123456789."; 
   var Char;
   for (i = 0; i < sText.length; i++) {
       Char = sText.charAt(i);
       if (ValidChars.indexOf(Char) == -1) {
          return false;
       }
    }
   return true;
}

As you can see I am just tring to extract the first number any help on this would be great.
 
What do you mean by the "From aspect"?

What does the data look like in your select?
Show me the value and text.

I suspect what you are attempting is to read the text and parse out the numerical value like pulling 100 out of 100-199 or 25 out of 25-49?


At my age I still learn something new every day, but I forget two others.
 
I did not blend this in with your code but you should be able to adapt it readily enough.
myval is the text string you want to check.
I first test if it contains a hyphen. If it does then I use indexOf to get the position of the hyphen to know the length of the string. If it contains no hyphen then I take the whole string minus the last character.
I did it this way because one of your entries was 200+ which had no hyphen.

I think it might be shorter code to use a regular expression but I am not good with them. Others might be able to show you but the bit of code below does what you are looking for.


Code:
if (myval.indexOf('-') != -1)
  var myout = myval.substring(0, myval.indexOf('-'));
else
  var myout = myval.substring(0, myval.length-1);
alert(myout);

At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top