PDA

View Full Version : Two dimensional array / sorting and combining / coding problem

shootingpandas
03-13-2012, 04:41 PM
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

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:

SortedArray = [
["000000","2"],
["111111","1"],
["111111","2"],
["111111","2"],
["222222","1"],
["222222","1"],
["222222","20"],
["222222","1"],
["333333","1"]
];

And from there to this:

["000000","2"],
["111111,"1","2"],
["222222","1","20","1"],
["333333","1"]
];

But I want to end up with this:

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

ckeyrouz
03-13-2012, 05:24 PM
For the first part to sort this array you need to pass a custom comparison method as follows:

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.

shootingpandas
03-13-2012, 06:27 PM
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).

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!

mrhoo
03-13-2012, 06:34 PM
Its always a challenge to write Array manipulations without the extended methods
like filter, map, reduce and indexOf.

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)

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

*/

Philip M
03-13-2012, 06:35 PM
This may be of assistance:-

<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

shootingpandas
03-13-2012, 06:42 PM
For the first part to sort this array you need to pass a custom comparison method as follows:

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!

shootingpandas
03-13-2012, 07:05 PM
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!

shootingpandas
03-13-2012, 07:55 PM
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.

shootingpandas
03-23-2012, 12:19 PM
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']
]