473,657 Members | 2,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.le ngth; 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.le ngth; i++) {
arrLookup[fbox.options[i].text] = fbox.options[i].value;
if (fbox.options[i].selected && fbox.options[i].value != "" &&
myTo.indexOf(fb ox.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 2420
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.go ogle.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.le ngth; 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.le ngth; i++) {
arrLookup[fbox.options[i].text] = fbox.options[i].value;
if (fbox.options[i].selected && fbox.options[i].value != "" &&
myTo.indexOf(fb ox.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(co mpare);

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************ *********@f14g2 000cwb.googlegr oups.com>,
dated Thu, 21 Oct 2004 20:28:04, seen in news:comp.lang. javascript, Jc
<go****@weinric hs.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.a sp


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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6
johkar wrote:
[snip]
Change arrTbox.sort();
to arrTbox.sort(co mpare);

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
4137
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) { return -1; } else
6
2252
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. ---------------------------(end of broadcast)--------------------------- TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faqs/FAQ.html
9
2603
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. For some more insight, not all policies will be prefixed with an "H", an in fact they could be prefixed with *any* letter combination, but they will always end with a number. When I retrieve them from the database I use "ORDER BY id" to get them...
1
7183
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 methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find nothing new or useful contained in this article. Short Review In part one I showed you some...
1
1283
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 2000 items. What I basically need to do is output these numbers to a PDF file I'm creating on the fly and I can only fit a certain amount of numbers per page. What I need to do is just output these on each page in numerical order vertically NOT...
77
3326
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 the size of input. I am not able to come up with anyting all all and doing: char* arr_of_pointers; seems like a completely wrong idea as this is a static array. I want to take the input and then decide how much memory I need and then malloc...
5
3179
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 search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort. First, the user is asked whether he/she wants to perform a search option, a sort operation, or exit the program. If...
1
2416
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 into the wrong direction as compared to what was intended. I think I am working on this program from last 1 month and it is still not done, Here is the intent:
5
4933
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 there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.