473,890 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sort array question

Hi, I'm using the following code to retrieve data from an xml file and
populate a javascript array. The data is then displayed in html table
form. I would like to then be able to sort by each column. Once the
array elements are split, what is the best way to sort them? Thank you.

//populate data object with data from xml file.
//Data is a comma delimited list of values
var jsData = new Array();
jsData[0] = {lib: "#field SUBLOOKUP_LIB#" ,doc: "#field SUBLOOKUP_DOC#" ,
com: "#field SUBLOOKUP_COM"} ;

//split up values for each element
var o = jsData[0];
o.lib = o.lib.split("," );
o.doc = o.doc.split("," );
o.com = o.com.split("," );

Nov 23 '05
21 3239
VK wrote:
Randy Webb wrote:
He may have thought he needed to sort arrays but sometimes a
better solution involves using some other mechanism than what
is originally though.
Mmm... Sorting is possible only on objects supporting IComparable
interface. Such are *for example* Array or String.
Object object doesn't have IComparable interface so a sentence
like "you need objects to sort" is meaningless to my *humble*
Huh? Arrays and Objects are *not* the same thing. We keep
telling you that and you keep ignoring it.

Do you care for one more attempt? Because this topic was
discussed here:
thread/039e84717c0978d 9/188f8dd6f2af8fe 7?hl=en#188f8dd 6f2af8fe7>

and I got nothing but explanations how stupid am I and that
the Object or Array performance are the same.

That is not what was said, though given your record for misunderstandin g
any explanations given to you I cannot be surprised by more evidence
that you do not (or will not) understand what is explained to you. It is
that seemingly wilful disregard for the efforts of others to improve
your understanding that is (IMO inevitably) turning the nature of the
responses you receive to your posts towards the exclusively negative.
After that actually I had to conduct my own research for the
best performance. Results are posted here.
You mean on this group? In the thread referenced about the most you said
on the subject was:-

<quote cite="news:11** *************** *****@g49g2000c wa.googlegroups .com"
Pre-declared Array object served with proper integer index values
gives me 6 sec time gain on my %windir%\temp\ directory (hundreds
of files and folders) in comparison with Hashtable and 3 sec over
mis-treated Array (used one time as hash, another time as array).
You may imagine what is even 3 sec for usability issue. To keep
this time gain though I have to stay with Array w/o temporary
transformations Array > Hash > Array.

- by which you appear to mean that using - anArray[integer] - gave
you a 6 second performance gain over using - anObject[string] - and
a 3 second gain over using - anArray[string].
Actually skip on explanations -
Yet again, assertions without anything to support them, or allow others
to directly discredit them.
I'm too tired of this crap.
Answer the questions you are asked and you may see the resolution of the
Other code is based on the proper understanding of Object and
Array nature, my code is based on a broken implementation exploit
which is not guaranteed to be presented everywhere.
OP is free to choose whatever he wants.

In the past you have posted examples of you attempts to time javascript,
and demonstrated your rational in drawing conclusions form those tests.
And at the time it was pointed out that the individual tests included so
much beyond what you were making statements about, and differed in what
they included beyond the subject of you conclusions, that any
conclusions drawn from the results would be invalid. The testing
methodology you demonstrated was so poor as to be useless, invalidating
you conclusions as a consequence.

As you have reacted to the criticism of you methodology by refusing to
post any more examples of your testing methods it is impossible to judge
whether thy have changed, but it seems reasonable to conclude that they
have not and so any deductions you now assert are backed by unshown
tests must reasonably be dismissed as based on fundamentally flawed
data. You can only change that impression by demonstrating that you
methodology has improved; by posting the code that does demonstrate the
points you are trying to make.

Consider the problem we have here: The question is which formulation of
property accessor most rapidly resolves into the value of interest. We
don't actually need to attempt to work out how fast any particular
property accessor is, only their relative performance. So given, for
example, the following two functions:-

function compA(a, b){
var x = a[1];
var y = b[1];
return (x < y) - (x > y);
function compB(a, b){
var x = a['1'];
var y = b['1'];
return (x < y) - (x > y);

- the only difference between them is that one uses an integer to access
the properties of an object and the other uses a string. Apart from that
they are identical. Theoretically any overheads in executing one will be
identical to the overheads in executing the other (given the same
arguments) and the _difference_ between their performance should be
entirely attributable to the use of the different types of property

Having narrowed the subject of the test down to just the feature of
interest it may be possible to draw conclusions about subject, all else
being equal. Unfortunately all else is not equal, for one thing we
cannot perform the test without the influence of background tasks on
multitasking operating systems. But still, lets give it a whirl and see
what happens.

The functions above are comparison functions for use with the
Array.prototype .sort method, as it is sorting arrays that is of interest
here. We will use them to sort arrays of objects and arrays of arrays,
where the objects being sorted have two named properties including the
one being referenced and the arrays have two elements (so the look-up
performance should not be that influenced by the size of the objects in
use). The pertinent property will be assigned a random number and the
random numbers in the objects in each array to be sorted will be in the
same order so that each array will be sorted in exactly the same way.
The number of calls to the comparison functions will be counted (which
will verify that all of the arrays are undergoing the same sorting
process) and the array will have 10000 elements so that the process
takes long enough to minimise the influence of the low resolution of
timers available to javascript. All of the difference measurements are
against a dot-notation property accessor accessing a named property. The
test page is:-

<script type="text/javascript">
var a1 = [];
var a2 = [];
var a3 = [];
var a4 = [];
var a5 = [];
var y = [];
var i = 10000;

for(var c = 0;c < i;++c){
y.push(Math.ran dom());

for(var c = 0;c < i;++c){
a1.push({'0':'x ','1':y[c]});
a3.push(['x', y[c]]);
a5.push({'0':'x ',n:y[c]});

function compA(a, b){
var x = a[1];
var y = b[1];
return (x < y) - (x > y);
function compB(a, b){
var x = a['1'];
var y = b['1'];
return (x < y) - (x > y);
function compC(a, b){
var x = a.n;
var y = b.n;
return (x < y) - (x > y);
<script type="text/javascript">
var start, end, dur, base;
var count = 0;

document.write( '<br>Array of '+i+' objects using obj.n:-<br>');
start = new Date().getTime( );
c = a5.sort(compC);
end = new Date().getTime( );
base = dur = (end - start);
document.write( 'Duration:- '+dur+'ms<br>') ;
document.write( 'Comparisons performed:- '+count+'<br>') ;
document.write( 'Property accessors resolved:- '+(count << 1)+'<br>');
document.write( 'Difference from obj.n per accessor:- '+
((base - dur)/(count << 1))+'<br>');
count = 0;

document.write( '<br>Array of '+i+' arrays using ar[\'1\']:-<br>');
start = new Date().getTime( );
c = a4.sort(compB);
end = new Date().getTime( );
dur = (end - start);
document.write( 'Duration:- '+dur+'ms<br>') ;
document.write( 'Comparisons performed:- '+count+'<br>') ;
document.write( 'Property accessors resolved:- '+(count << 1)+'<br>');
document.write( 'Difference from obj.n per accessor:- '+
((base - dur)/(count << 1))+'<br>');
count = 0;

document.write( '<br>Array of '+i+' arrays using ar[1]:-<br>');
start = new Date().getTime( );
c = a3.sort(compA);
end = new Date().getTime( );
dur = (end - start);
document.write( 'Duration:- '+dur+'ms<br>') ;
document.write( 'Comparisons performed:- '+count+'<br>') ;
document.write( 'Property accessors resolved:- '+(count << 1)+'<br>');
document.write( 'Difference from obj.n per accessor:- '+
((base - dur)/(count << 1))+'<br>');
count = 0;

document.write( '<br>Array of '+i+' objects using obj[\'1\']:-<br>');
start = new Date().getTime( );
c = a2.sort(compB);
end = new Date().getTime( );
dur = (end - start);
document.write( 'Duration:- '+dur+'ms<br>') ;
document.write( 'Comparisons performed:- '+count+'<br>') ;
document.write( 'Property accessors resolved:- '+(count << 1)+'<br>');
document.write( 'Difference from obj.n per accessor:- '+
((base - dur)/(count << 1))+'<br>');
count = 0;

document.write( '<br>Array of '+i+' objects using obj[1]:-<br>');
start = new Date().getTime( );
c = a1.sort(compA);
end = new Date().getTime( );
dur = (end - start);
document.write( 'Duration:- '+dur+'ms<br>') ;
document.write( 'Comparisons performed:- '+count+'<br>') ;
document.write( 'Property accessors resolved:- '+(count << 1)+'<br>');
document.write( 'Difference from obj.n per accessor:- '+
((base - dur)/(count << 1))+'<br>');

As each comparison function performs two property accesses the count of
the number of calls to the comparison function is doubled in order to
calculate the difference in performance of the particular property
accessor type. Differences that are negative are worse performance than
dot-notation property accessor, and positive results are better
performance. All results are in milliseconds.

The notation - ar - is used to indicate that the subject of the property
accessor is an array and - obj - to indicate when it is an object. The
following are the results of running the page in IE 6, Mozilla 1.5 and
Opera 8.5 on a 500MHz PIII on the Windows 98 operating system:-

500MHz PIII Win 98
IE 6 Mozilla 1.6 Opera 8.5
obj.n ------ ------ ------
ar['1'] -0.0028256 -0.0001109 -0.0021326
ar[1] -0.0033992 -0.0002661 -0.0006042
obj['1'] -0.0035054 -0.0001109 -0.0019548
obj[1] -0.0038666 -0.0001330 -0.0013862

The result that would be predicted form a literal implementation of ECMA
262 would be that dot-notation would be fastest, and so all of the
numbers shown should be negative, and that accessing with a string
should be faster than accessing with an integer, with Array accessing
being slightly slower than Object accessing.

We find that all the numbers are negative, so dot-notation is the
fastest form of property accessor here. And with the exception of Opera
string access is coming up faster than integer access, but (again with
the exception of Opera) Array access appears fractionally faster than
Object access.

Baring in mind that a 500MHz CPU is slow by any current standard and you
would expect to see the measured performance differences in property
accessors getting smaller as CPU speeds increased, the differences
between bracket-notation performance on a single browser range from
0.0015284 (biggest, Opera) to 0.0001552 (smallest, Mozilla)
milliseconds. That is, form 1.5 to 0.15 _millionths_ of a second. That
means that if you want to account for a 6 second difference in outcome
by just changing property accessors you need to be doing between
4,000,000 and 40,000,000 property accessing operations (and more is the
CPU is faster). Notice that IE will sort an random array of 10,000
elements in about 250,000 calls to the comparison function, and very
considerably fewer for smaller arrays.

But now to the "all else being equal" part of the problem. If you want
to attribute performance differences between property accessors to the
javascript implementation, and make deductions from the results as to
the "true" nature of that implementation, you would have to demonstrate
that any differences were entirely attributable to the internal workings
of the implementation and could not be manifestations of other factors.
However, Switch OS and things come out very differently. Repeating the
tests on the same class of CPU; PIII, and the same versions of the same
browsers, but switching to Windows 2000 (and a slightly faster CPU out
of necessity) we get:-

800Mhz PIII Win 2000
IE 6 Mozilla 1.6 Opera 8.5
obj.n ------ ------ ------
ar['1'] -0.0007224 -0.0000200 -0.0008154
ar[1] -0.0110437 0.0000022 -0.0001028
obj['1'] -0.0048314 -0.0000222 -0.0007799
obj[1] -0.0115749 0.0000244 -0.0004254

Given the faster CPU (500Mhz to 800MHz) we would expect the timings to
be about two thirds of the originals, they are not. IE is all over the
place, with some values worse and others better, but at least the
relative positions remain the same; strings are better than numbers and
Array access faster than Object access. On Mozilla we have a total turn
around, number access figures have gone positive and are now better than
dot-notation, but the overall performance difference is much better than
could be accounted for by CPU speed alone. The relative positions of
Opera's results are much the same as the first set but again the
performance difference is greater than could be accounted for by the CPU

I would expect that if these tests were repeated on other combinations
of CPU and OS we would see different results again. I would anticipate
that Windows XP and a P4 potentially reverse the results across the
board. In the same way as speed testing string comparisons against
regular expressions favours string comparison on PIII Win 98 machines
and regular expressions on Win XP P4 machines. That was an unexpected
phenomenon but makes sense because P4s have hardware optimisation that
favours the sort of things that regular expressions (and video
decompression) need to do and the Windows XP operating system was
written with a knowledge of their potential. Which also explains why the
results above exceed the differences expected by the change of CPU
speed. Windows 98 was written before the PIII existed and cannot fully
exploit any hardware acceleration it provides, while Windows 2000 can
take advantage of its potential (but may not). Windows 2000 will not
exploit the P4's special features, while windows XP may.

The bottom line is that you can time things, and maybe see an obvious
performance benefit in a particular action over another (given testing
on a wide range of browser, CPU and OS combinations indicates that the
perceived benefit is manifest in all/sufficient, and not actually
harmful to some), but you cannot make deductions about the internal
details of javascript implementations from those measurements. You
cannot say that any observed difference is a result of how the
implementation alone is coded because you cannot separate the execution
of the implementation form the OS/CPU environment in which you execute
it, and that combination makes a difference.

On the other hand, this whole thing is a quest for performance in the
wrong place. The differences in the performance of property accessors
are a mater of nanoseconds, you have to be doing millions of them before
it is an issue. Specifically, if you assert that you are sorting arrays
of only "hundreds" of elements and changing the type of property
accessor used makes a difference as large as 6 seconds then the
algorithm you are using is catastrophicall y bad. It should be to that
that you should be looking to achieve better performance not splitting
hairs over property accessors.

Nov 23 '05 #21
On 2005-11-17, ye*****@yahoo.c om <ye*****@yahoo. com> wrote:
However, you should read and adhere to <http://jibbering.com/faq/#FAQ2_3>,
especially <http://www.jibbering.c om/faq/faq_notes/clj_posts.html> , if you
desire my further help.
thanks for the hot tip!
As I wrote before, you need to post _real_ input and
output data if one is to find a viable transformation algorithm.

is this sufficient? This is as real as I can give you.

sadData[0] =
{lib:"string1,s tring2,string3, string4",doc:"0 1,02,03,04",com :"blah,blah,bla h,blah"};

and this is the format I would like to change it to:

happyData[0] = {lib:"string1", doc:"01",com:"b lah"};
happyData[1] = {lib:"string2", doc:"02",com:"b lah"};
happyData[2] = {lib:"string3", doc:"03",com:"b lah"};
happyData[3] = {lib:"string4", doc:"04",com:"b lah"};

use split and a for loop.


Nov 23 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the number of records. Both these numbers are known
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort (Bubble, insertion, selection..). I have an array of int's and am passing them to each sort function.
by: Steve Wasser | last post by:
I need to sort a two-dimensional array. Each day I process a file with 9 comma-delimited fields, and varying amount of records (lines). I want to pull in each line, split it according to the comma into a two dimensional array (Perl has it f'r crissake), and sort by one of the fields (Purchase Order #). Trouble is, Array.Sort only supports single dimension arrays. Originally I was thinking of making a jagged array, pulling out the Purchase...
by: Jan Smith | last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a clear answer to my question. Maybe it's not in Array.Sort. Here's the question: I initialize an array X with the values 28 142 3 17 225. I can sort this array in ascending order and it will return 3 17 28 142 225. But I want a method that will return the sort order, not the array in sorted
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
by: Balaskas Evaggelos | last post by:
Hi, does anyone know how i can sort a multi-dimensional array by a specific field ? for example i want to sort arr where n=2, but i need the data of every array to follow that order. example array: arr
by: ^cypis^ vel. SQ9JTI | last post by:
Hi, i need your help. I have to prepare a homework, easy program, which will be sorting the values from txt file and writing the solution to another txt file. It has to be a bucket sort. Have anyone a source code for this sample? Many thanks in advance! Regards, Luke
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the format was different. Basically you can sort a class having members LastName (string), FirstName (string) and Age (int) according to whether you want to sort by Last Name, First Name or Age, using the .Sort function
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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...
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...
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...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
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.