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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a list the starts with numbers but has letters 2

Status
Not open for further replies.

mattquantic

Programmer
Mar 28, 2004
196
GB
Hi

I use 3 functions to sort a select list that's been populated by dynamically. (people can add and remove items)

Was all working great until I need to apply it to a list of water tank values.

My list (which I can't change) is:
10 Ltrs
25 Ltrs
50 Ltrs
75 Ltrs
100 Ltrs
250 Ltrs

I thought about maybe spiting each item into 3 objects in an array (id,number part, letter part) and a then sorting by sorting by the numbers part. Then re-creating the list from the sorted array.

Is this the right direction? If so - how would I do that?

Thanks,
Matt






------------------------------------------------------------
The three functions I use:

// sort function - ascending (case-insensitive)
function sortFuncAsc(record1, record2) {
var value1 = record1.optText.toLowerCase();
var value2 = record2.optText.toLowerCase();
if (value1 > value2) return(1);
if (value1 < value2) return(-1);
return(0);
}

// sort function - descending (case-insensitive)
function sortFuncDesc(record1, record2) {
var value1 = record1.optText.toLowerCase();
var value2 = record2.optText.toLowerCase();
if (value1 > value2) return(-1);
if (value1 < value2) return(1);
return(0);
}

function sortSelect(selectToSort, ascendingOrder) {

// default to ascending sort
if (arguments.length == 1) ascendingOrder = true;

// copy options into an array
var myOptions = [];
for (var loop=0; loop<selectToSort.options.length; loop++) {
myOptions[loop] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
}

// sort array
if (ascendingOrder) {
myOptions.sort(sortFuncAsc);
} else {
myOptions.sort(sortFuncDesc);
}

// copy sorted options from array back to select box
selectToSort.options.length = 0;
for (var loop=0; loop<myOptions.length; loop++) {
var newoption = new Option(myOptions[loop].optText,myOptions[loop].optValue);
selectToSort.add(newoption);
}

}
 
You could use SPLIT or SUBSTRING to get the numerical components out of these values and then process them, I'd personally use SPLIT.

Although you could also use the fact that in an OPTION tag you could get away with something like :

<OPTION VALUE="250">250 Ltrs</OPTION>

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Hi - unfortunatly the option value is a reference from a database and not related to the sequence.

Can you tell me how to order an array on a specific oject name within it?

Matt
 
>var value1 = record1.optText.toLowerCase();
>var value2 = record2.optText.toLowerCase();
[tt]var value1 = [red]parseInt([/red]record1.optText.toLowerCase()[red],10)[/red];
var value2 = [red]parseInt([/red]record2.optText.toLowerCase()[red],10[/red];[/tt]
 
You could pad all the values with enough zeros to make the numbers all the same length, then sort them normally. When done, remove the extra zeros. On a short list that is probably the easiest and fastest.

On a long list, I would break the strings into the numerical part and text part into an array, sort, then reassemble them.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
An obvious typo of mine, here a re-take.
[tt]
var value1 = [red]parseInt([/red]record1.optText.toLowerCase()[red],10)[/red];
var value2 = [red]parseInt([/red]record2.optText.toLowerCase()[red],10)[/red];[/tt]
 
Hi merrlinn and ggriffit

Thanks soo much for replying.

I'm looking at the parseInt mehtod as there are only 15 or so records.

It's working great.

Thanks for saving me some time developing something that would have worked but not been the most efficient choice.

Awesome!

Matt
 
Sorry - thank tsuji - I saw yours and merrlinn posts as one.

Cheers and thanks everyone.
 
No problem. Just a short supplementary note: If the measure remains uniform (even with that degree of freedom of Ltrs, L, litres, Litres according to the caprice of the users---and it seems to be, as a sort according to the numeric suffice), toLowerCase() part would play no relevant role. Hence, you sure can spare it, if efficiency is a concern.
[tt]
var value1 = parseInt(record1.optText,10);
var value2 = parseInt(record2.optText,10);[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top