# Thread: how do you sort a combined array

1. Registered User
Join Date
Jun 2010
Posts
2

## how do you sort a combined array

This is my coding:

var company=new Array("Kestrel Moon","BB")
var femalePri=new Array(89,0)

for(var i=0;i<2;i++){
var companyTotal=company[i].concat(finalTotal[i]);
document.write(""+companyTotal+"");

this displays:

Kestrel Moon 89
BB 0

I want it so that it displays in ascending order of number

BB 0
Kestrel Moon 89

The problem is that when I do a normal sort array, it puts the company names in alphabetical order and the price stays as it is and if I sort the numbers in ascending order, the numbers go in ascending order but the company names stays where ther are.

I want it so that numerically its in acscending order and when the price is placed in ascending order, their company names go with their prices, can this be done and if it can, can you please show me an example?

thank you

2. ## Question ...

It can be done, but I have a question ...

How long are you into the developmental process?
Can the two Arrays be combined into a single array like
Code:
```// var company=new Array("Kestrel Moon","BB")
// var femalePri=new Array(89,0)
var companyFemale = [
"Kestrel Moon,89",
"BB,0"
];```
It could possibly simplify the task if data could be arranged this way.

If not, the task can still be accomplished, but with a bit more code.

3. Registered User
Join Date
Oct 2006
Posts
939
HTML Code:
```<SCRIPT type="text/javascript">
//Concat the 2 arrays with the number going first, name second. Then, sort by parseInt()

var company=new Array("Kestrel Moon","BB")
var femalePri=new Array(89,0)
var companyTotal=[]

for(i=0;i<2;i++){	 companyTotal[i]=femalePri[i]+'~'+company[i];}

companyTotal.sort(function(a,b){return parseInt(a) - parseInt(b)});

document.write( companyTotal.join('<br>') );
document.write( '<P>' );
//each element of companyTotal can now be split into an Array in
// order to write it with name first

for(i=0;i<companyTotal.length;i++){companyTotal[i]=companyTotal[i].split('~');
document.write(companyTotal[i][1]+' '+companyTotal[i][0]+'<br>')}
</SCRIPT>```

4. ## Something to consider ...

Using 'justinbarneskin's sort function and my idea of combining the master array,
you could do something like this (or modify further for the remaining fields) ...
Code:
```<html>
<title> Master Array Display </title>
<body>
<script type="text/javascript">

var masterArray = [
'Kestrel Moon: £,0,1165,475,97,0,98',
'BB: £,89,1231,380,43,180,85',
'Whatever You Drive: £,34,1052,210,23,450,43',
'Discount Drivers: £,291,1667,310,302,189,93',
'Indirect Curve: £,0,804,599,45,0,0',
'Swindel: £,187,1010,210,35,32,90',
'Captain: £,0,510,100,19,0,40',
'Saintly Drivers: £,0,890,0,0,0,155',
'Ipswich Union: £,0,490,0,0,0,0',
'Young Drivers: £,203,803,0,78,0,82',
'Atlee: £,107,923,200,81,92,70',
'Good Deal: £,434,1690,300,112,493,47',
'Nice Price: £,0,775,0,487,0,30',
'Insure For Less: £,998,1435,0,49,0,59',
'Instant Insure: £,43,1223,424,113,68,96'
];
// following for testing purposes only
// document.write('Original Array<p>');
// document.write(masterArray.join('<br>'));

function NumSort(a,b) {	return parseInt(a) - parseInt(b) }

function ShowInfo( srtInfo,flds ) {
var str = '';
var tmp = [];
var temp = [];
for (var i=0; i<masterArray.length; i++) {
tmp = masterArray[i].split(',');
str = '';
for (var j=0; j<flds.length; j++) { str += ','+tmp[flds[j]]; }
temp.push(str.substring(1));
}
switch (srtInfo) {
case 'aa' : temp.sort(); break;
case 'na' : temp.sort(NumSort); break;
case 'nd' : temp.sort(NumSort).reverse(); break;
default: alert('Error has occured: '+srtInfo); break;
}
//  document.getElementById('masterInfo').innerHTML = temp.join('<br>');
// above is test display before table below created
str = '<table border="1">';
for (var i=0; i<temp.length; i++) {
tmp = temp[i].split(',');
str += '<tr><td>'+tmp[0]+'</td><td>'+tmp[1]+'</td></tr>';
}
str += '</table>';
document.getElementById('masterInfo').innerHTML = str;
}

</script>
<br>
<button onclick="document.getElementById('masterInfo').innerHTML = ''">Clear Display</button>
<button onclick="document.getElementById('masterInfo').innerHTML = masterArray.join('<br>')">
Display Master </button>
<br>
<button onclick="var arr=[0,1];ShowInfo('aa',arr)">Fields 0,1 ascending</button>
<button onclick="var arr=[1,0];ShowInfo('na',arr)">Fields 1,0 ascending</button><br>
<button onclick="var arr=[1,0];ShowInfo('nd',arr)">Fields 1,0 decending</button><br>
<p>
<div id="masterInfo"></div>
</body>
</html>```
Have fun and good luck!

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.13539 seconds
• Memory Usage 2,880KB
• Queries Executed 15 (?)
Template Usage (34):
• (2)bbcode_code
• (1)bbcode_html
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (4)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (4)postbit
• (4)postbit_onlinestatus
• (4)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (70):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete