473,382 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Sorting an Array - Ignore first 3 characters

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[i].text] = tbox.options[i].value;
arrTbox[i] = tbox.options[i].text;
myTo += tbox.options[i].value;
}
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i < fbox.options.length; i++) {
arrLookup[fbox.options[i].text] = fbox.options[i].value;
if (fbox.options[i].selected && fbox.options[i].value != "" &&
myTo.indexOf(fbox.options[i].value) <= -1) {
arrTbox[tLength] = fbox.options[i].text;
tLength++;
}
else {
arrFbox[fLength] = fbox.options[i].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;
}
}
Jul 23 '05 #1
6 2404
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/de...6jsmthsort.asp

Jul 23 '05 #2
johkar wrote:
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.
Jul 23 '05 #3
johkar wrote:
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.
Jul 23 '05 #4
no********@msn.com (johkar) wrote in message news:<e5**************************@posting.google. com>...
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[i].text] = tbox.options[i].value;
arrTbox[i] = tbox.options[i].text;
myTo += tbox.options[i].value;
}
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i < fbox.options.length; i++) {
arrLookup[fbox.options[i].text] = fbox.options[i].value;
if (fbox.options[i].selected && fbox.options[i].value != "" &&
myTo.indexOf(fbox.options[i].value) <= -1) {
arrTbox[tLength] = fbox.options[i].text;
tLength++;
}
else {
arrFbox[fLength] = fbox.options[i].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
Jul 23 '05 #5
JRS: In article <10*********************@f14g2000cwb.googlegroups. com>,
dated Thu, 21 Oct 2004 20:28:04, seen in news:comp.lang.javascript, Jc
<go****@weinrichs.com> posted :
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/de...cript56/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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6
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.
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
6
by: Dennis Gearon | last post by:
This is what has to be eventually done:(as sybase, and probably others do it) http://www.ianywhere.com/whitepapers/unicode.html I'm not sure how that will affect LIKE and REGEX. ...
9
by: Dylan Parry | last post by:
Hi folks, I have a database that contains records with IDs like "H1, H2, H3, ..., Hn" and these refer to local government policy numbers. For example, H1 might be "Housing Policy 1" and so on....
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
1
by: LittlBUGer | last post by:
Hello. First of all I'm programming in VB.NET/ASP.NET doing a page for a website. Now, to my question.... I have a simple array of integer numbers (15 characters in length) which can hold up to...
77
by: arnuld | last post by:
1st I think of creating an array of pointers of size 100 as this is the maximum input I intend to take. I can create a fixed size array but in the end I want my array to expand at run-time to fit...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
1
by: arnuld | last post by:
On Mon, 29 Sep 2008 15:16:56 +0100, Ben Bacarisse wrote: An abstract overview of my own program by Ben had helped me focus on the design issue first. I came to know that whole problem went...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.