Useful Array Functions

Got from here http://www.hunlock.com/blogs/Mastering_Javascript_Arrays

by Terrance Smith

HTML

<body><h4><a name="quickIDX37"></a>Useful Prototypes</h4>
<p>With prototypes you can extend the Array object and include any additional
functionality you wish.  For instance, the Array.sort() method sorts alphabetically
by default but adding a numerical sort is as simple as including the following snippet
of code in your toolbox.

</p><pre class="code">Array.prototype.sortNum = function() {
   return this.sort( function (a,b) { return a-b; } );
}
</pre>
<p>All this does is make sortNum a method of Array just like sort and splice and
join and all the other default methods.  Any array can access it, even arrays
that haven't been assigned to variables…
</p><pre class="code">document.writeln([5,8,12,50,25,80,93].sortNum()); // outputs 5,8,12,25,50,80,93
</pre>
<p>Since the Javascript Array object is already so rich, there's not much left
that needs to be done.  However, there are a few snippets people have found helpful.

</p><h4><a name="quickIDX38"></a>Useful Prototypes: Array.sortNum()</h4>
<p>As stated above, just use the sortNum() method when you want to sort an
array numerically instead of alphabetically.
</p><pre class="code">Array.prototype.sortNum = function() {
   return this.sort( function (a,b) { return a-b; } );
}
</pre>

<h4><a name="quickIDX39"></a>Useful Prototypes: Array.find(searchStr)</h4>
<p>Array.indexOf() is a nice method but this extension is a little more
powerful and flexible.   First it will return an array of all the indexes it
found (it will return false if it doesn't find anything).  Second in addition
to passing the usual string or number to look for you can actually pass a
regular expression, which makes this the ultimate Array prototype in my book.
</p><pre class="code">Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i&lt;this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
     ...

CSS

VAR {
    color: #880000;
    font-family: courier new;
    font-size: 10pt;
    font-style: normal;
}
.copyright {
    font-family: verdana;
    font-size: 9pt;
}
.header {
    background-color: white;
    border-bottom: 1px dotted black;
    height: 40px;
    min-width: 993px;
    opacity: 1;
    padding: 5px;
    width: 99%;
}
.highlight {
    background-color: yellow;
    text-decoration: underline overline;
}
.artBlock {
    background: url("http://www.hunlock.com/images/blue-fade-bg-left.jpg") repeat-y scroll 0 0 transparent;
    border: 2px solid black;
    border-radius: 10px 10px 10px 10px;
    font-family: Cambria,Georgia,"Palatino Linotype",Palatino;
    font-size: 1em;
    margin: 5px;
    min-height: 175px;
    padding: 5px;
}
.headBlock {
    font-family: verdana;
    font-size: 1.5em;
    font-weight: bold;
    margin-bottom: 3pt;
}
.headBlock A {
    color: black;
    text-decoration: none;
}
.footer {
    background-color: white;
    border-top: 1px dotted black;
    font-family: "Palatino Linotype",Palatino,Georgia,serif;
    font-size: 0.8em;
    height: 40px;
    padding: 5px;
    text-align: center;
    width: 99%;
}
.indexBar {
    border: 1px solid black;
    border-radius: 5px 5px 5px 5px;
    padding: 5px;
    text-align: left;
}
.indexTitle {
    background-color: black;
    border-radius: 3px 3px 3px 3px;
    color: white;
    font-family: verdana;
    font-size: 1.1em;
    text-align: center;
}
.indexBar A {
    background: url("http://www.hunlock.com/images/arrow_fat_down.gif") no-repeat scroll left top transparent;
    font-family: verdana;
    font-size: 0.9em;
    padding-left: 12px;
    text-decoration: none;
}
.indexBar A:hover {
    background-color: blue;
    color: white;
}
.rssBar {
    border: 1px solid black;
    border-radius: 5px 5px 5px 5px;
    padding: 5px;
    text-align: left;
}
.rssTitle {
    background-color: black;
    border-radius: 3px 3px 3px 3px;
    color: white;
    font-family: verdana;
    font-size:...

JavaScript

Array.prototype.sortNum = function() {
    return this.sort(function(a, b) {
        return a - b;
    });
};
Array.prototype.find = function(searchStr) {
    var returnArray = false;
    var i = 0;
    for (i = 0; i < this.length; i++) {
        if (typeof(searchStr) == 'function') {
            if (searchStr.test(this[i])) {
                if (!returnArray) {
                    returnArray = [];
                }
                returnArray.push(i);
            }
        } else {
            if (this[i] === searchStr) {
                if (!returnArray) {
                    returnArray = [];
                }
                returnArray.push(i);
            }
        }
    }
    return returnArray;
};
Array.prototype.shuffle = function() {
    
    for (var rnd, tmp, i = this.length; i; rnd = parseInt(Math.random() * i), tmp = this[--i], this[i] = this[rnd], this[rnd] = tmp);
};
Array.prototype.compare = function(testArr) {
    if (this.length != testArr.length) return false;
    var i = 0;
    for (i = 0; i < testArr.length; i++) {
        if (this[i].compare) {
            if (!this[i].compare(testArr[i])) return false;
        }
        if (this[i] !== testArr[i]) return false;
    }
    return true;
};