Skip to content

Instantly share code, notes, and snippets.

@Ausjorg
Last active September 27, 2019 16:00
Show Gist options
  • Save Ausjorg/c872042f391626321dbed05cac2188ad to your computer and use it in GitHub Desktop.
Save Ausjorg/c872042f391626321dbed05cac2188ad to your computer and use it in GitHub Desktop.
How to sort and Array of Integers with JavaScript.
A = [5,6,3,3,1,2,4]
// Remove Duplicates
A = new Set(A)
A = [...A]
// Ascending Order 1 first
A = A.sort(function(a, b){return a-b});
// Descending Order 1 last
A = A.sort(function(a, b){return b-a});
// Turn Integer to Binary
N = (N).toString(2);
// Break string into array
N = N.split("")
// One line if statement
(a>b) ? doThis : doThis
if (lemons) document.write("foo gave me a bar");
// Fastest for loop
for (var i = 0, len = myArray.length; i < len; i++) {
}
// Sort objects by value
const order = state => ({
desc: (opt) => {
return state.sort((a, b) => (a[opt] < b[opt]) ? 1 : -1)
},
asc: (opt) => {
return state.sort((a, b) => (a[opt] > b[opt]) ? 1 : -1)
}
});
function two(x = 1, y = 1, genFn) {
let newArray = [];
let subArray = [];
let count = [0,0];
for (let i = 0; i < (x*y); i++) {
count[0]++
subArray.push(genFn ? genFn(count[1], count[0]) : undefined)
if (count[0] === x) {
newArray.push(subArray)
subArray = []
count = [0, count[1] += 1]
}
}
return newArray;
}
console.log(two(5, 3, () => []))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment