Sorting an Array - Ignore first 3 characters

J

johkar

I am in the process of editing the below code I found online, I have a
from multi-select list and a to multi-select list. Before rewriting
the to list, I want to sort it but ignore the "F - " and the "R - ".
Ideas???
F - A200000 Some Text Here
R - H205200 Some Other Text

John

function move(fbox, tbox) {
var arrFbox = new Array();
var arrTbox = new Array();
var myTo=""
var arrLookup = new Array();
var i;
for (i = 0; i < tbox.options.length; i++) {
arrLookup[tbox.options.text] = tbox.options.value;
arrTbox = tbox.options.text;
myTo += tbox.options.value;
}
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i < fbox.options.length; i++) {
arrLookup[fbox.options.text] = fbox.options.value;
if (fbox.options.selected && fbox.options.value != "" &&
myTo.indexOf(fbox.options.value) <= -1) {
arrTbox[tLength] = fbox.options.text;
tLength++;
}
else {
arrFbox[fLength] = fbox.options.text;
fLength++;
}
}
//arrFbox.sort();
arrTbox.sort();
//fbox.length = 0;
tbox.length = 0;
var c;
//for(c = 0; c < arrFbox.length; c++) {
//var no = new Option();
//no.value = arrLookup[arrFbox[c]];
//no.text = arrFbox[c];
//fbox[c] = no;
//}
for(c = 0; c < arrTbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrTbox[c]];
no.text = arrTbox[c];
tbox[c] = no;
}
}
 
F

Fred Oz

johkar said:
I am in the process of editing the below code I found online, I have a
from multi-select list and a to multi-select list. Before rewriting
the to list, I want to sort it but ignore the "F - " and the "R - ".
Ideas???
F - A200000 Some Text Here
R - H205200 Some Other Text
[snip]

Seems to me you want to trim 4 characters "F - " and "R - ". I haven't
played with the JS sort function, but some act funny if you have
leading spaces (but it may not be an issue here, suck it and see).

I would trim the required number characters from the start of each
string and append them to the end:

'F - A200000' becomes 'A200000F - '

then sort them, then trim the same number from the end and put them
back onto the start. Again, watch for truncated spaces.

Cheers, Fred.
 
Z

Zifud

johkar said:
I am in the process of editing the below code I found online, I have a
from multi-select list and a to multi-select list. Before rewriting
the to list, I want to sort it but ignore the "F - " and the "R - ".
Ideas???
F - A200000 Some Text Here
R - H205200 Some Other Text
[snip]

I gotta ask - where is the data coming from? If this is a one-of, sort
it using any method you like - Word or Excel, or in *nix pipe together
some cut/paste/sort commands - then create your option list and it's done.

If the data is coming from a database, do it on the server. I'm certain
an SQL newsgroup will give you a query to do what you want. Then your
list is already sorted before it gets to your page.

I can't for the life of me see why you would be sorting a list of stuff
like this on the client.

Zif.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Thu, 21 Oct 2004 20:28:04, seen in Jc
Sure, specify your own sort function to use. The Array's sort method by
default just does a basic sort, so if you want anything fancier, you
can write a custom function to compare two values that ignores the
first three characters, and pass that in to the sort method as a
parameter.

See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/j
s56jsmthsort.asp

However, the compare function is called more than o(N) times when
sorting a N-element array; and the required function, though not
difficult, takes non-trivial time.

Therefore, if N is sufficiently large (and that may not be very big) it
should be quicker to use a RegExp or substring method to park the first
three characters at the end of the value in time o(N), do a standard
sort, and restore the data.
 
J

johkar

I am in the process of editing the below code I found online, I have a
from multi-select list and a to multi-select list. Before rewriting
the to list, I want to sort it but ignore the "F - " and the "R - ".
Ideas???
F - A200000 Some Text Here
R - H205200 Some Other Text

John

function move(fbox, tbox) {
var arrFbox = new Array();
var arrTbox = new Array();
var myTo=""
var arrLookup = new Array();
var i;
for (i = 0; i < tbox.options.length; i++) {
arrLookup[tbox.options.text] = tbox.options.value;
arrTbox = tbox.options.text;
myTo += tbox.options.value;
}
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i < fbox.options.length; i++) {
arrLookup[fbox.options.text] = fbox.options.value;
if (fbox.options.selected && fbox.options.value != "" &&
myTo.indexOf(fbox.options.value) <= -1) {
arrTbox[tLength] = fbox.options.text;
tLength++;
}
else {
arrFbox[fLength] = fbox.options.text;
fLength++;
}
}
//arrFbox.sort();
arrTbox.sort();
//fbox.length = 0;
tbox.length = 0;
var c;
//for(c = 0; c < arrFbox.length; c++) {
//var no = new Option();
//no.value = arrLookup[arrFbox[c]];
//no.text = arrFbox[c];
//fbox[c] = no;
//}
for(c = 0; c < arrTbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrTbox[c]];
no.text = arrTbox[c];
tbox[c] = no;
}
}



Thank you all for your responses. This is what I came up with. It
seems to work during brief testing. Let me know if you see flaws.

Change arrTbox.sort();
to arrTbox.sort(compare);

function compare(a,b){
a = a.substr(4);
b = b.substr(4);
if(a < b) return -1;
if(b < a) return 1;
if(a == b) return 0;
}

As far as Zifud's question as to why I would ever want to do this on
the client. Consider two multiple select lists where you transfer
items back and forth. I realize there are different schools of
thought on what you should do on the client. Zifud, you probably
subscribe to hitting the server for most everything, I am more open
depending on the situation. It just comes down to your audience,
environment and business requirements. Thanks again.

John
 
Z

Zifud

johkar wrote:
[snip]
Change arrTbox.sort();
to arrTbox.sort(compare);

function compare(a,b){
a = a.substr(4);
b = b.substr(4);
if(a < b) return -1;
if(b < a) return 1;
if(a == b) return 0;
}

As far as Zifud's question as to why I would ever want to do this on
the client. Consider two multiple select lists where you transfer
items back and forth. I realize there are different schools of
thought on what you should do on the client. Zifud, you probably
subscribe to hitting the server for most everything,

Not at all, I just couldn't see the reason for doing it. Your scenario
seems quite reasonable, now I understand.

As Dr. J suggests, your bubble sort will run into performance issues at
some point, but if your list my not get long enough (maybe it's always
less than 10 items or such).

Another suggestion is to give each option a value equivalent to the key
you wish to sort on, then use that to sort the array. When you post
the selected & sorted list back to the server, use the text rather than
the value at the server end.

Cheers, Zif.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top