Can't sort items in an array matrix

G

Greg Raven

OK, I'm stumped. I want to create a page that allows visitors to enter
torque (dyno) readings at various RPM as a prelude to calculating the
optimum transmission shift points. But, I want to make certain that
the data are in ascending RPM order. The problem is that I can't get
array.sort to work on my rpm/torque/hp matrix.

Here's where I am:

// This works
var dyno = [7000, 2000, 4500, 900];
dyno.sort(function(a, b) {
return a - b;
});
print(dyno);

// This does not work -- no sort effect whatsoever
var dyno = [[7000, 50], [2000, 88], [4500, 109], [900, 77]];
dyno.sort(function(a, b) {
return a - b;
});
print(dyno);

The only thing I found that will work is including a Quicksort routine
in place of the standard (bubble?) sort, but that seems long-winded
and inelegant.

Is there a way of getting array.sort to work on array matrices, and if
so, how?

Thanks.
 
T

Thomas 'PointedEars' Lahn

Greg said:
// This does not work -- no sort effect whatsoever
var dyno = [[7000, 50], [2000, 88], [4500, 109], [900, 77]];
dyno.sort(function(a, b) {
return a - b;
});

This comparator will be passed two adjacent array elements for `a' and `b'.
The computation for the return value (which indicates whether `a' should be
placed before `b' or vice-versa) converts the values to number. If the
values are references to Array instances with more than one element, the
number representations of those objects is `NaN' (the IEEE-754 not-a-number
value). As `NaN - NaN' results in `NaN', the return value is neither a
negative number (a "<" b, place b after a), nor 0 (b "=" a, don't change
order) or a positive number (a ">" b, place b before a).

So what you are passing to sort() is not `undefined' and does not qualify as
a "consistent comparison function" as defined in ES3F.

As a result, the behavior of sort() is implementation-defined. In your case
the implementation appears to ignore the value passed to sort() as if
`undefined' (or nothing) was passed.

See ES3F, section 15.4.4.11, for details.
Is there a way of getting array.sort to work on array matrices, and if
so, how?

It is possible. You need to modify your comparator so that it implements an
ordering function for those Array instance references that are elements of
the array. IOW, the question you need to answer is: Is [7000, x] greater
than, less than, or equal to [2000, y]?

And if you mean by "to work with matrices" that it should sort the elements
of the Array-type elements, then you need to call sort() on those "inner"
Array instances instead and then implement the ordering function for the
sorted "inner" instances.


PointedEars
 
G

Greg Raven

  dyno.sort(function(a, b) {
      return a[0] == b[0] ? a[1] - b[1] : a[0] - b[0];
  });

Stefan,

Thanks, I just arrived at almost that same code myself -- after two
days!

var dyno = [[7000, 50], [2000, 88], [4500, 109], [900, 77]];
dyno.sort(function(a, b) {
return a[0] - b[0];
});
print(dyno);

There will never be a (valid) case in which there are two different hp
readings at the same RPM point.
 
J

Jorge

  dyno.sort(function(a, b) {
      return a[0] == b[0] ? a[1] - b[1] : a[0] - b[0];
  });

Stefan,

Thanks, I just arrived at almost that same code myself -- after two
days!

var dyno = [[7000, 50], [2000, 88], [4500, 109], [900, 77]];
dyno.sort(function(a, b) {
    return a[0] - b[0];});

print(dyno);

There will never be a (valid) case in which there are two different hp
readings at the same RPM point.

sort= function (a,b) {
return ((typeof a === "number") ? a-b : a[0]-b[0]);
}

[3,2,1].sort(sort)
-> [1, 2, 3]

[[3,1],[1,1],[0,1]].sort(sort)
-> [[0, 1], [1, 1], [3, 1]]

Or:

newSorter= function (p) {
return (typeof p === "number" ?
function (a,b) { return a-b; } :
function (a,b) { return a[0]-b[0]; });
}

[3,2,1].sort(newSorter(0))
-> [1, 2, 3]

[[3,1],[1,1],[0,1]].sort(newSorter([]))
-> [[0, 1], [1, 1], [3, 1]]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top