Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-13-2012, 03:41 PM   PM User | #1
shootingpandas
New to the CF scene

 
Join Date: Mar 2012
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
shootingpandas is an unknown quantity at this point
Two dimensional array / sorting and combining / coding problem

Hi everyone

I'm having a hard time, wrapping my head around an array sort+combine function.

I have an array which would look somewhat (these are simplified numbers) like this

Code:
TestArray = [
["222222","1"],
["222222","1"],
["222222","1"],
["333333","1"]
["111111","1"],
["000000","2"],
["111111","2"],
["111111","2"],
["222222","20"],
];
I got it as far as to be sorted like this:
Code:
SortedArray = [
["000000","2"],
["111111","1"],
["111111","2"],
["111111","2"],
["222222","1"],
["222222","1"],
["222222","20"],
["222222","1"],
["333333","1"]
];
And from there to this:
Code:
BadlyMergedArray = [
["000000","2"],
["111111,"1","2"],
["222222","1","20","1"],
["333333","1"]
];
But I want to end up with this:
Code:
MergedArray = [
["000000","2"],
["111111","1","2"],
["222222","1","20"],
["333333","1"]
];
So the first part of the inner arrays is the first item to be sorted and combined by.
After that, the smaller numbers have to do practically the same, while staying in the correct order under the first sort.

I'm only a spare time programmer, and my code is probably one of the most confusing pieces of code ever written.
I am assuming that this is some fairly easy stuff for one of you guys. I can post my code, but I think there's a much easier solution to it, so I wanted to see if someone can point me in the right direction, or show me how this is properly done.

Thanks so much! here's to hoping!

patrick/shootingpandas
shootingpandas is offline   Reply With Quote
Old 03-13-2012, 04:24 PM   PM User | #2
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
For the first part to sort this array you need to pass a custom comparison method as follows:

Code:
var testArray = [
								["222222","1"],
								["222222","1"],
								["222222","1"],
								["333333","1"]
								["111111","1"],
								["000000","2"],
								["111111","2"],
								["111111","2"],
								["222222","20"],
							];
			
			function multiSorter(a,b)
			{
			    //sorting ascending
			    if (a[0] < b[0])
			    {
			    	return -1;   
			    }
			    if(a[0] > b[0])
			    {
			        return 1;
			    }
			    if(a[0] == b[0])
			    {
			        if (a[1] < b[1])
				    {
				    	return -1;   
				    }
				    if(a[1] > b[1])
				    {
				        return 1;
				    }
				    return 0;
			    }
			}
			
			var sortedArray = testArray.sort(multiSorter);
However the question about merging is not clear for me.

You need to merge what?

Post more details about the second point for me to be able to help you.
__________________
Software and cathedrals are much the same - first we build them, then we pray.
ckeyrouz is offline   Reply With Quote
Users who have thanked ckeyrouz for this post:
shootingpandas (03-13-2012)
Old 03-13-2012, 05:27 PM   PM User | #3
shootingpandas
New to the CF scene

 
Join Date: Mar 2012
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
shootingpandas is an unknown quantity at this point
Quote:
Originally Posted by ckeyrouz View Post
However the question about merging is not clear for me.

You need to merge what?

Post more details about the second point for me to be able to help you.
Thanks a ton ! This is really awesome. and looks so easy now you wouldn't believe how many nested functions I'm using for this still there's two things which seem weird (put it into the [] brackets below).

Your script returns

000000,2,1, [where does the 1 come from? maybe from the lost "333333" part]
111111,2,
111111,2,
222222,1,
222222,1,
222222,1,
222222,20
[script seems to lose the "333333" part]

so this is sorted.

about the merge: basically the script needs to lose all the duplicates, combining them in one.

"000000","2"
stays as is

"111111","1"
"111111","2"
"111111","2"
needs to be combined/merged to
"111111","1","2"

"222222","1"
"222222","1"
"222222","1"
"222222","20"
combine / merge to
"222222","1","20"

"333333","1"
as is

does this clarify it?

thanks a ton for helping me out here!

Last edited by shootingpandas; 03-13-2012 at 07:01 PM.. Reason: grammar ;)
shootingpandas is offline   Reply With Quote
Old 03-13-2012, 05:34 PM   PM User | #4
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
Its always a challenge to write Array manipulations without the extended methods
like filter, map, reduce and indexOf.


Code:
var testarray= [
	["222222", "1"], ["222222", "1"], ["222222", "1"],
	["333333", "1"], ["111111", "1"], ["000000", "2"],
	["111111", "2"], ["111111", "2"], ["222222", "20"],
],
tem, t, O= {};
testarray.sort(function(a, b){
	if(a[0]== b[0]) return a[1]-b[1];
	return a[0]-b[0];
});
mergeloop:
while(testarray.length){
	tem= testarray.shift();
	t= tem[0];
	if(!O[t]) O[t]= [t];
	for(var i= 1, L= O[t].length; i<L; i++){
		if(O[t][i]=== tem[1]) continue mergeloop;
	}
	O[t].push(tem[1]);
}
for(var p in O){
	if(O.hasOwnProperty(p)) testarray.push(O[p]);
}

/* returned value: (Array)
Code:
[
	['000000','2'],	
	['111111','1','2'],
	['222222','1','20'],
	['333333','1']
]
*/

Last edited by mrhoo; 03-13-2012 at 05:39 PM.. Reason: condensed return
mrhoo is offline   Reply With Quote
Users who have thanked mrhoo for this post:
shootingpandas (03-13-2012)
Old 03-13-2012, 05:35 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
This may be of assistance:-

Code:
<script type = "text/javascript">

function sortMultiDimensional(a,b) {    // this sorts the array using the second element 
return ((a[1] < b[1]) ? -1 : ((a[1] > b[1]) ? 1 : 0));}

var arr = [[4,8],[5,9],[2,10],[3,2]]; // the multidimensional array
//arr.sort(); // normal sort
arr.sort(sortMultiDimensional); // sort using custom function
for(var i=0;i<arr.length;i++) {  
document.write(arr[i].toString()+"<br>");
}

</script>
"Merely corroborative detail, intended to add artistic verisimilitude to an otherwise bald and unconvincing narrative". W S Gilbert
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 03-13-2012, 05:42 PM   PM User | #6
shootingpandas
New to the CF scene

 
Join Date: Mar 2012
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
shootingpandas is an unknown quantity at this point
Quote:
Originally Posted by ckeyrouz View Post
For the first part to sort this array you need to pass a custom comparison method as follows:

Code:
			function multiSorter(a,b)
			{
			    //sorting ascending
			    if (a[0] < b[0])
			    {
			    	return -1;   
			    }
			    if(a[0] > b[0])
			    {
			        return 1;
			    }
			    if(a[0] == b[0])
			    {
			        if (a[1] < b[1])
				    {
				    	return -1;   
				    }
				    if(a[1] > b[1])
				    {
				        return 1;
				    }
				    return 0;
			    }
			}
Just to understand what this does: the function pulls two items (a,b) out of the array and starts comparing these. If the item [0] is equal in both items it continues with comparing the [1] part of the nested arrays.

But how does "return" work for the sort command? The only part of the sort command I really can grasp is the standard "a - b" and "b - a" ascending, descending sort. Where can I educate myself on that? Most infos I can find are deeper problems of functions working with the sort command. I just don't know how to call the sort command properly.

Thanks a ton on that as well!

Last edited by shootingpandas; 03-13-2012 at 06:57 PM..
shootingpandas is offline   Reply With Quote
Old 03-13-2012, 06:05 PM   PM User | #7
shootingpandas
New to the CF scene

 
Join Date: Mar 2012
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
shootingpandas is an unknown quantity at this point
Quote:
Originally Posted by mrhoo View Post
Its always a challenge to write Array manipulations without the extended methods
like filter, map, reduce and indexOf. ...
Thanks so much! This is a really awesome piece of script.
I'm trying to test out the last kinks, and will report back asap!

EDIT: At the moment I'm combining ckeyrouz' sort and mrhoo's merge script and this seems to do the trick for me. I still have some testing to do though.

You guys are unbelievable! thank you so much. As I thought this seems so easy, once it's there. Still trying to wrap my head around it. I'll report back!

Last edited by shootingpandas; 03-13-2012 at 06:58 PM.. Reason: last question, has probably been cleared!
shootingpandas is offline   Reply With Quote
Old 03-13-2012, 06:55 PM   PM User | #8
shootingpandas
New to the CF scene

 
Join Date: Mar 2012
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
shootingpandas is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
This may be of assistance:-
...
Thank you Philip, for the effort! At the moment I'm trying to get the other sort & merge scripts to work. And this seems to do the trick. I'll report back.

Last edited by shootingpandas; 03-13-2012 at 06:58 PM..
shootingpandas is offline   Reply With Quote
Old 03-23-2012, 11:19 AM   PM User | #9
shootingpandas
New to the CF scene

 
Join Date: Mar 2012
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
shootingpandas is an unknown quantity at this point
Hey guys, thank you so much. The combined version works perfect for me! Thanks so much for helping me out here!

I have one more question though, because I'm trying to add more functionality to it.

Im using mrhoo's merging script, and can't quite figure out the way it works
Because what I'm trying to do is exchange the first separator with a different string after the merging is done, and was thinking about implementing this in the merging part.

Or would this be easier afterwards?

I'm trying to end up with something like this:


[
['000000/t2'],
['111111/t1','2'],
['222222/t1','20'],
['333333/t1']
]

or

[
['000000'/t'2'],
['111111'/t'1','2'],
['222222'/t'1','20'],
['333333'/t'1']
]

instead of

[
['000000','2'],
['111111','1','2'],
['222222','1','20'],
['333333','1']
]

thanx again so much!
shootingpandas is offline   Reply With Quote
Reply

Bookmarks

Tags
array, combine, javascript, multidimensional, sort

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:23 PM.


Advertisement
Log in to turn off these ads.