Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Sort Problem Got Me Beat! 3

Status
Not open for further replies.

starblood

Technical User
Feb 28, 2001
63
0
0
GB
Hi All,

This problem has gone beyond the realms of my programming knowledge.

I have an array that I have ordered numerically:

var data = new Array(200812151000, 200812151315, 200812161000, 200812161315, 200812161930, 200812171000, 200812171315, 200812181000, 200812181315, 200812191000, 200812191315);

For your info each figure corresponds to a date YYYYMMDDHHMM

What I would like to do is reorder the array if the red figures are consecutive (15 => 16, 16 => 17) AND the green figures remain the same. So to use the date reference, gather together consecutive day events occurring at the same scheduled time.

Now I know it can probably be done as an Array sort. If you look for a comparative difference of exactly 10000 then that would probably hit the target, I just cannot program it with my knowledge.

By the way the correct ordered output for the above array would be as follows (as you can see, the groups themselves would appear based on the numerical value of the first number in the sequence):

200812151000
200812161000
200812171000
200812181000
200812191000

200812151315
200812161315
200812171315
200812181315
200812191315

200812161930
 
Thanks Feherke for you reply and input. Unfortunately your code doesn't do the trick.
Code:
<HTML>
<HEAD>
<HEAD>
<BODY>
<SCRIPT> 

var theArray = new Array(200812151000, 200812151315, 200812161000, 200812161315, 200812161930, 200812171000, 200812171315, 200812181000, 200812181315, 200812191000, 200812191315); 
   
document.write ("<b>Original array:</b>" + theArray); 
document.write ("<br>"); 
theArray.sort(function(a,b){a=a+'';b=b+'';return a.substr(0,6)+a.substr(8,4)+a.substr(6,2)>b.substr(0,6)+b.substr(8,4)+b.substr(6,2)});	

document.write ("<b>Custom sorted array:</b> " + theArray); 
</SCRIPT>
</BODY>
</HTML>

And yes I've spent the whole morning reading documentation about Array.sort. My head still hurts from it and I'm none the wiser.
 
The idea of Feherke is sound. The implement is like this.
[tt]
theArray.sort(function(a,b) {
var c=a.toString().substr(8,4)+a.toString().substr(0,8);
var d=b.toString().substr(8,4)+b.toString().substr(0,8);
return (parseFloat(c)-parseFloat(d));
});[/tt]
 
Thanks tsuji, that certainly seems to output the array in the order I require. Much appreciated.

And thanks once again to you Feherke.
 
Hi

Then I do not understand the whole thing. My code, starblood's implementation and tsuji's variation gives me the same order as the provided sample, "correct ordered output".


Feherke.
 
The reason is, your data[0] or their theArray[0], say, is number, not string, yet; and this is an error of the kind which blocks the completion of the logic.
[tt] theArray[0].substr(0,6) //error[/tt]
 
Hi

Yes, that is why I "converted" a and b to [tt]String[/tt] :
Code:
a=a+'';b=b+'';
Hmm... It works in FireFox. I have no other browser available for testing now.


Feherke.
 
I see. Then it comes down to the comparison > returning true or false. The function should return =ve,0,+ve whereas comparison returns only true or false which might be os-dependent (1,0) or (-1,0) or the worse senario
[tt] alert(parseInt(1>2,10)+"\n"+parseInt(1<2,10));[/tt]
returning NaN and NaN.
 
Hi

Good point. Probably you are right. ( Although your [tt]alert()[/tt] sample gives the same result, [tt]Boolean[/tt] seems to work in [tt][2,1].sort(function(a,b){return a>b})[/tt] . )

Thanks for the explanation.

Feherke.
 
Feherke, that gets me intrigue after I put the sorting function in the form you had proposed. Now, that gives me an instance to show where using the input parameter name is faulty. This shows what happens (apart from some revised substr() distribution irrelevant to the demo).
[tt]
theArray.sort(function(a,b){
var c=a+'';
var d=b+'';
return (parseFloat(c.substr(8,4)+c.substr(0,6)+c.substr(6,2))-parseFloat(d.substr(8,4)+d.substr(0,6)+d.substr(6,2)));
});
[/tt]
This is incorrect and erronous.
[tt]
theArray.sort(function(a,b){
a=a+'';
b=b+'';
return (parseFloat(a.substr(8,4)+a.substr(0,6)+a.substr(6,2))-parseFloat(b.substr(8,4)+b.substr(0,6)+b.substr(6,2)));
});[/tt]
 
Hi

For this code I got the outputs mentioned in the comments :
Code:
x=[2,1]
alert(typeof x[0])  [gray]// number[/gray]
x.sort(
  function(a,b) {
    a=a+''; b=b+''
    alert(typeof a) [gray]// string[/gray]
    return a>b
  }
)
alert(typeof x[0])  [gray]// number[/gray]
alert(typeof a)     [gray]// undefined[/gray]
So I assumed it is not wrong to do it like I did. Anyway, I will review my coding style for the future.

Feherke.
 
Hi Feherke,

Just to let you know I checked your orginal code in FireFox3, and it did indeed output the correct order. However IE6, IE7, Opera9 and Netscape 7 didn't.

So your suspicions about the browser implementation were correct.

This has been an eyeopener for me, as I thought as long as the code was supported, it would execute in the same way regardless.
 
Hi

Eyeopener... Quite precise expression in my case too.

One little curiosity remains : what my last ( 9 Sep 08 3:41 ) sample code outputs in other browsers. Opera and Konqueror outputs the same, so I see no incorrectness in my stringification.

By the way, the sorting also works in Konqueror.


Feherke.
 
Hi Feherke,

Tried your code in Opera9, Netscape7, IE6, Safari. They all produced the same output as you did:

number
string
number
undefined
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top