Skip to content

Commit c652e5d

Browse files
Added Array functions
1 parent 03f4f4e commit c652e5d

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/Array.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
This file contain Javascript functions related to arrays
3+
Copyright © 2006-2018 WinterNet Studio, Allan Jensen (www.winternet.no). All rights reserved.
4+
*/
5+
6+
(function( Jjs, $, undefined ) {
7+
8+
Jjs.Array = {};
9+
var me = Jjs.Array;
10+
11+
/**
12+
* Sort a "two-dimensional" array by a value in the subarrays
13+
*
14+
* @param {array} array - Array to be sorted
15+
* @param {string} sortBy - Name of key in subarray with value to sort by
16+
* @param {boolean} descending - Set to true to sort descendingly instead of ascendingly
17+
* @return {array}
18+
*/
19+
me.arrayColumnSort = function(array, sortBy, descending) {
20+
return array.sort(dynamicSort(sortBy, descending));
21+
};
22+
function dynamicSort(property, descending) {
23+
var sortOrder = 1;
24+
if (descending) {
25+
sortOrder = -1;
26+
property = property.substr(1);
27+
}
28+
return function (a,b) {
29+
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
30+
return result * sortOrder;
31+
}
32+
}
33+
34+
/**
35+
* Remove duplicates in an array
36+
*
37+
* Source: http://www.martienus.com/code/javascript-remove-duplicates-from-array.html
38+
*
39+
* @param {array} array - Array to be sorted
40+
* @return {array}
41+
*/
42+
me.uniqueArray = function(array) {
43+
var r=new Array();
44+
o:for(var i=0,n=array.length;i<n;i++) {
45+
for(var x=0,y=r.length;x<y;x++) {
46+
if(r[x]==array[i]) {
47+
continue o;
48+
}
49+
}
50+
r[r.length]=array[i];
51+
}
52+
return r;
53+
};
54+
55+
}( window.Jjs = window.Jjs || {}, jQuery ));

0 commit comments

Comments
 (0)