Alphabetical array sort - lowercase first - javascript

Given the array :
var myArray = ['d','a','k','v','z','A','t','G']
If I were to use :
myArray.sort()
The result would be :
myArray = ['A','G','a','d','k','t','v','z']
Is there a way of modifying the sort function or the output to put the uppercase letters at the end of the array like this :
myArray = ['a','d','k','t','v','z','A','G']
Either pure Javascript or jQuery would be OK.

Test the first character and apply compare:
var myArray = ['d', 'a', 'k', 'v', 'z', 'A', 't', 'G', 'V'];
myArray.sort(function (a, b) {
if (a[0] === a[0].toLocaleLowerCase() && b[0] === b[0].toLocaleLowerCase() ||
a[0] === a[0].toLocaleUpperCase() && b[0] === b[0].toLocaleUpperCase()) {
return a.localeCompare(b);
}
if (a[0] === a[0].toLocaleLowerCase()) {
return -1;
}
return 1;
});
document.write('<pre>' + JSON.stringify(myArray, 0, 4) + '</pre>');

Here's an example of the array sort using the compare function:
myArray.sort(function(a, b){
var test = a.charCodeAt(0) <= 90 && b.charCodeAt(0) <= 90;
if(test) return a.charCodeAt(0)-b.charCodeAt(0);
else if(a.charCodeAt(0) <= 90) return 1;
else if(b.charCodeAt(0) <= 90) return -1;
else return a.charCodeAt(0)-b.charCodeAt(0);
});

You should use custom compare function myArray.sort(compareFunction). Inside of the function you can use
conditions like if(a === a.toUpperCase()) and so on to create logic that you need.

var myArray = ['d','a','k','v','z','A','t','G'];
_.each(myArray, function(item){
if(item.charCodeAt(0) > 96){
smallArr.push(item)
}
else{
captArray.push(item)
}
});
smallArr.sort();
captArray.sort();
myArray = [];
myArray.push.apply(myArray, smallArr.concat(captArray));

once you have got this array
myArray = ['a','d','k','t','v','z','A','G']
do this to get it sorted by myArray = ['A','G','a','d','k','t','v','z']
var tempArray = myArray.join("").split(/(?=[A-Z])/);
myArray = tempArray.slice(1).concat( tempArray [0].split("") );

Related

Sum of elements in array using recursion even if they are string

Have to create a function that return the sum of the element in the array but if the array is
["a","b","c"] // output : abc
So far I have
function calculateSumRecursion(array) {
//your code
if (array.length === 0 ) {
return 0
}
return array[0] + calculateSumRecursion(array.slice(1))
}
I found out how to calculate the sum of all numbers using recursion but when it's an array of string like
array = ["a","b","c"]
it returns me
// abc0
because of the if statement.. is there any way to say
if (array.length === 0) return nothing instead of a 0 (that work only when it's an array of number?)
You just need to return the only value in the array when the length is 1, rather than waiting until you get a length of 0. That way you are always summing compatible types (numbers or strings). Note that you still need a test for a 0 array length in case the function gets called with an empty array. In this case you need to choose what to return; as requested, it is 0.
function calculateSumRecursion(array) {
if (array.length === 0) {
return 0;
}
if (array.length === 1) {
return array[0];
}
return array[0] + calculateSumRecursion(array.slice(1))
}
console.log(calculateSumRecursion([1, 2, 3, 4, 5]));
console.log(calculateSumRecursion(['a', 'b', 'c']));
console.log(calculateSumRecursion([]));
let arr = [1,2,3,4,5] // output : abc
let sum = calculateSumRecursion(arr);
function calculateSumRecursion (arr) {
return arr.length ? arr.pop() + calculateSumRecursion(arr) : 0;
}
Slice version
let arr = [1,2,3,4,5] // output : abc
let sum = calculateSumRecursion(arr);
function calculateSumRecursion (arr) {
return arr.length ? arr[0] + calculateSumRecursion(arr.slice(1)) : 0;
}
Change return 0 to return "" which will add an empty string to the sum.
You have returned 0 when the array is empty.
Now, you are doing string operations so it is needed to return empty value (not zero) so it will be affordable to return "".
function calculateSumRecursion(array) {
return array.length === 0 ? "" : array[0] + calculateSumRecursion(array.slice(1));
}
There's a way easier way to do this:
function calculateSumRecursion(array) {
var out = array[0];
for (let i = 1; i < array.length; i++) {
out = out + array[i];
}
return out;
}
Return empty string on recursion base case. Just replace your return 0 to return ''.
const array = ['a', 'b', 'c'];
function calculateSumRecursion(array) {
if (array.length === 0) {
return '';
}
return array[0] + calculateSumRecursion(array.slice(1));
}
console.log(calculateSumRecursion(array));
If you are want to work with number also then check array length for zero as well as one.
const array = ['a', 'b', 'c', 'e'];
const array2 = [];
const array3 = [1, 2, 3];
function calculateSumRecursion(array) {
const rec =
array.length === 1
? array[0]
: array.length >= 1 && array[0] + calculateSumRecursion(array.slice(1));
return array.length === 0 ? 0 : rec;
}
console.log(calculateSumRecursion(array));
console.log(calculateSumRecursion(array2));
console.log(calculateSumRecursion(array3));

How to sort based on incomplete criteria?

First I tried passing my own function to Array.sort, but it doesn't sort correctly. Notice how 'c' comes before 'a' in the result, even though the case if (b == 'a' && a == 'c') is handled correctly.
These data are just for example. My actual data is not to be alphabetically sorted. It must use the logic illustrated in the a_before_b and b_before_a functions.
Since I only have conditions to determine the relative ordering of SOME (NOT all) pairs of elements, there may be multiple valid orderings of elements. I just need to produce ANY valid ordering, where valid means does not contradict any of my conditions (which are defined in the a_before_b and b_before_a functions).
const sorted = ['a', 'b', 'c', 'd']; // I do NOT have access to this
const unsorted = ['c', 'd', 'a', 'b'];
const a_before_b = (a, b) => {
if (a == 'a' && b == 'd') return true;
if (a == 'b' && b == 'c') return true;
}
const b_before_a = (a, b) => {
if (b == 'a' && a == 'c') return true;
if (b == 'b' && a == 'c') return true;
}
const mySortingFunction = (a, b) => {
if (a_before_b(a, b)) return -1;
if (b_before_a(a, b)) return 1;
return 0;
}
// doesn't produce correct sorting
console.log(unsorted.sort(mySortingFunction)); // [ 'c', 'a', 'd', 'b' ]
Then I tried writing my own sort from scratch. But it enters an infinite loop and I don't know why.
const sorted = ['a', 'b', 'c', 'd'];
const unsorted = ['c', 'd', 'a', 'b'];
const a_before_b = (a, b) => {
if (a == 'a' && b == 'd') return true;
if (a == 'b' && b == 'c') return true;
}
const b_before_a = (a, b) => {
if (b == 'a' && a == 'c') return true;
if (b == 'b' && a == 'c') return true;
}
const findAnUnsortedElement = array => {
for (let [i, element] of Object.entries(array)) {
i = +i;
const a = element;
const b = array[i + 1];
if (b === undefined) return 'SORTING_COMPLETE';
if (!a_before_b(a, b)) console.log(a, 'should not be before', b);
if (b_before_a(a, b)) console.log(b, 'should be before', a);
if (!a_before_b(a, b) || b_before_a(a, b)) return a;
}
}
// from w3schools
function move(arr, old_index, new_index) {
while (old_index < 0) {
old_index += arr.length;
}
while (new_index < 0) {
new_index += arr.length;
}
if (new_index >= arr.length) {
var k = new_index - arr.length;
while ((k--) + 1) {
arr.push(undefined);
}
}
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
return arr;
}
// enters infinite loop, never returns
const myCustomSort = array => {
while (findAnUnsortedElement(array) != 'SORTING_COMPLETE') {
const element = findAnUnsortedElement(array);
const index = array.findIndex(el => el == element);
console.log('moving', element);
array = move(array, index, index + 1);
console.log(array);
}
return array;
}
console.log(myCustomSort(unsorted));
const unsorted = ['c', 'd', 'a', 'b'];
const sorted = unsorted.sort();
It should work I'm not sure what's your issue.
The algorithm in the answer I gave earlier on, and which you (first) accepted, is really based on a heuristic.
For a sorted output to be guaranteed to not have any violations, you could treat this problem as a graph problem. Whenever two values can make a comparison that gives true (with either comparator function), then that pair represents an edge in the graph.
If the order is consistent, then there must be one value that is the least among the others, otherwise you would have a cycle.
So with that knowledge we can determine for each node in the graph how long the longest path is to such a least node. When you find the longest distance to such a least node, you can use the length of that path as an absolute order indication.
Here is an implementation:
class Node {
constructor(value) {
this.value = value;
this.prev = new Set;
this.order = 0; // No order yet
}
orderWith(other) {
if (other === this) return;
if (a_before_b(this.value, other.value) || b_before_a(other.value, this.value)) {
other.prev.add(this);
} else if (a_before_b(other.value, this.value) || b_before_a(this.value, other.value)) {
this.prev.add(other);
}
}
setOrder(path = new Set) {
// Use recursion to find length of longest path to "least" node.
if (this.order) return; // already done
if (path.has(this)) throw "cycle detected";
let order = 1;
for (let prev of this.prev) {
prev.setOrder(path.add(this));
order = Math.max(order, prev.order + 1);
}
this.order = order; // If order is 1, it is a "least" node
}
}
const a_before_b = (a, b) => {
if (a == 'a' && b == 'd') return true;
if (a == 'b' && b == 'c') return true;
}
const b_before_a = (a, b) => {
if (b == 'a' && a == 'c') return true;
if (b == 'b' && a == 'c') return true;
}
function mySort(arr) {
// Create a graph: first the nodes
let nodes = {}; // keyed by values in arr
for (let value of arr) nodes[value] = nodes[value] || new Node(value);
// Then the edges...
for (let i = 0; i < arr.length; i++) {
for (let j = i+1; j < arr.length; j++) {
nodes[arr[i]].orderWith(nodes[arr[j]]);
}
}
// Set absolute order, using the longest path from a node to a "least" node.
for (let node of Object.values(nodes)) node.setOrder();
// Sort array by order:
return arr.sort((a, b) => nodes[a].order - nodes[b].order);
}
const sorted = ['a', 'b', 'c', 'd'];
const unsorted = ['c', 'd', 'a', 'b'];
console.log(mySort(unsorted));
Maybe something like this
const sorted = ['a', 'b', 'c', 'd']; // I do NOT have access to this
const unsorted = ['c', 'd', 'a', 'b'];
const a_before_b = (a, b) => {
if (a == 'a' && b == 'd') return true;
if (a == 'b' && b == 'c') return true;
if (a == 'a' && b == 'c') return true;
}
const b_before_a = (a, b) => {
if (b == 'a' && a == 'c') return true;
if (b == 'b' && a == 'c') return true;
}
const mySortingFunction = (a, b) => {
if (a_before_b(a, b)) return -1;
if (b_before_a(a, b)) return 1;
return 0;
}
// doesn't produce correct sorting
console.log(unsorted.sort(mySortingFunction));

there is an string array A , and an string array B . I want to delete elements in A which are not in B

I think i messed somewhere, Here is my code.
var flag;
for (i = 0; i < A.length; i++)
{
flag = 0;
for (j = 0; j < B.length; j++)
{
if (A[i].indexOf(B[j]) != -1)
{
flag = 1;
}
}
if (flag == 0)
{
A.splice(i, 1);
}
}
It gives output not as per my need
Someone please Help me out
I would do the job like this;
//returns intersection of multiple arrays
Array.prototype.intersect = function(...a) {
return [this,...a].reduce((p,c) => p.filter(e => c.includes(e)));
};
var a = [0,1,2,3,4,5],
b = [4,5,6,7,8,9];
a = a.intersect(b);
console.log(a);
You could use a function which generates first an object with all characters as properties and take it as hashtable for the filtering of array1.
function deleteSome(array1, array2) {
var o = Object.create(null);
array2.forEach(function (a) {
o[a] = true;
});
return array1.filter(function (a) {
return this[a];
}, o);
}
var a = 'abcdefgh'.split(''),
b = 'banana'.split('');
console.log(deleteSome(a,b));
Technically, array "a" should have only elements which are present in array "b".
var a = [1,2,3,4];
var b = [4,5,6];
var new_a = [];
a.map(function(v,i,a){
if(b.indexOf(v) !== -1){
new_a.push(v);
}
});
console.log(new_a); //[4]
By this way i can filter as many arrays as you want.
var a = ['A', 'A', 'R', 'S', 'M', 'D', 'E']
var b = ['C', 'X', 'D', 'F']
//you can add as many arrays as you want
/*var c = ['O', 'P', 'D', 'Q']
var d = ['R', 'D', 'D', 'Z']*/
var arrays = [a,b, /*c , d */];
var result = arrays.shift().filter(function(v) {
return arrays.every(function(a) {
return a.indexOf(v) !== -1;
});
});
console.log(JSON.stringify(result));

Combining some array items

I'm kind of new to functional programming and I try to do the following: Let's say I have an array of values: ['a','b-','c'] and I want that every item which ends with a '-' is merged with the following array entry: ['a','b-c'].
Of course I can do this by making a for-loop:
var test = ['a', 'b-', 'c'], result = [];
for (var i=0;i<test.length;i++) {
var curr = test[i];
if (curr.endsWith('-')) {
curr += test[i+1];
i++;
}
result.push(curr);
}
But how can one do this without the for loop?
To be honest, the way you programmed is probably the most efficient way to do this.
However, here's another option:
var test = ['a', 'b-', 'c'],
result = test.join().replace(/-,/g, '').split(',');
console.log(result);
This joins all elements into a string: 'a,b-,c', removes all occurrences of '-,' > 'a,bc', then splits the string back up into an array with the desired result, as you can see in the output.
This can be fool-proofed a bit, by changing the separator used in the join / split:
var test = ['a', 'b-', 'c'],
separator = '||',
result = test.join(separator)
.replace(new RegExp('-' + separator, 'g'), '')
.split(separator);
One possible approach (with .reduce):
var arr = ['a', 'b-', 'c'];
var trans = arr.reduce(function(acc, cur) {
if (acc.length && acc[acc.length - 1].slice(-1) === '-') {
acc[acc.length - 1] += cur;
}
else {
acc.push(cur);
}
return acc;
}, []);
This can also be achieved using Array.prototype.map:
var test = ['a', 'b-', 'c'];
var result = test.slice().map(function (x, i, a) {
if (x.endsWith("-") && a[i+1]) {
var r = x + a[i+1]; // Join this and the next element in the array
a.splice(i, 1); // Remove the next element from the array
return r;
}
return x;
}).filter(function (x) {
return typeof x !== 'undefined';
}); // Since the array comes back with a different length and some undefined elements, remove those. Thanks #Cerbrus for pointing this out
console.log(test, result, result.length); // ["a", "b-", "c"] ["a", "b-c"] 2
This way will work for multiple dashed elements in a row, and if the last element has a dash, uses Array.forEach
var test = ['a', 'b-', 'c-'], result = [], next = "";
test.forEach(function(curr) {
if (curr.endsWith('-')) {
next += curr;
if (curr == test[test.length-1]) {
result.push(next);
}
}else {
result.push(next + curr);
next = "";
}
});
document.write(result);
Another map + filter one. Most likely slower, as filter add's another iteration through the array, but works as the original does (which is probably not what the OP wants when there are multiple -'s in a row).
var test = ['a', 'b-', 'c-', 'd', 'e'], result = [];
result = test
.map((curr, i, array) => (curr.endsWith('-') && array[i + 1] !== undefined) ? curr + array[i+1] : curr)
.filter((curr, i, arr) => (i>0 && arr[i-1].length > 1 && curr.length === 1) ? false : true)
document.write(result);
Didn't read all answers, so sry if I repeat sth. that has already been said.
Functional programming doesn't mean that there is always a predefined function that does exactly what you intend to; or a combination of some.
Sometimes it is better to write a simple short utility-function than abusing the ones that are already there.
How about some "problems" like multiple dashed-values next to each other, or at the end of the list? How do you want to handle these cases?
This would be my implementation:
function combineDashedStrings(arr){
var result = [], pending = ""
for (var i=0; i<arr.length; i++) {
var curr = pending + arr[i];
pending = curr.endsWith("-") && curr || ""; //multiple concats
//pending = !pending && curr.endsWith("-") && curr || ""; //single concat
pending || result.push(curr);
}
//pending && result.push(curr); //add last item if it contains a dash
return result
}
combineDashedStrings(['a', 'b-', 'c-', 'd', 'e-']);
feel free to switch the commented lines/options

how to find an element in one of three array and then return the parent array name?

I am trying to check if an element exists in any one of three arrays. I don't know how to return the name of the array where the element was found. Can anyone direct me into the right direction please.
I have coded a function which takes the element in search as its argument and then returns the array name:
var arr1 = ['a','b','c','d'];
var arr2 = ['e','f','g','h'];
var arr3 = ['i','j','k','l'];
function chkElem(elem)
{
var id = elem;
var isFound = null;
if(arr1.indexOf(id) || (arr2.indexOf(id) || (arr3.indexOf(id))))
{
isFound = ????
}
return isFound;
}
I am uncertain how to assign the parent array name to 'isFound' variable.
Thanks.
You should never use "variable names" in your function logic. Instead, make the arrays properties of an object and return the property name:
var arrays = {
"arr1": ['a','b','c','d'],
"arr2": ['e','f','g','h'],
"arr3": ['i','j','k','l']
};
for (var name in arrays)
if (arrays[name].indexOf(id) > -1)
return name;
return null;
Or, even better, use an array of arrays to search in and return the index:
var arrays = [
['a','b','c','d'],
['e','f','g','h'],
['i','j','k','l']
];
for (var i=0; i<arrays.length; i++)
if (arrays[i].indexOf(id) > -1)
return i;
return -1;
Test one-by-one:
if (arr1.indexOf(id) > -1) {
isFound = arr1;
} else if (arr2.indexOf(id) > -1) {
isFound = arr2;
} else if (arr3.indexOf(id) > -1) {
isFound = arr3;
}
Alternatively, create a multi-dimensional array:
var arr = [
['a','b','c','d'],
['e','f','g','h'],
['i','j','k','l']
];
var isFound = null;
for (var i = 0; i < arr.length; i++) {
if (arr[i].indexOf(elem) > -1) {
isFound = arr[i];
break;
}
}
Firstly, be careful of the indexOf() trap - if it fails to find the requested string, it will return -1 - which is a truthy - so you need to check explicitly like so:
if (arr1.indexOf(id) != -1)
not
if (arr1.indexOf(id))
The truthy/falsy concept also means that, if your string is the first element in the array, and so indexOf() returns false, that is a falsy, and so your condition will actually fail even though a match was made!
Secondly, you cannot return the name of the array - or, to be more precise, the name of the variable that references it in the JS memory. You can either:
1) return the array itself
if (arr1.indexOf(id) != -1) return arr1;
2) store your arrays in a central object and return the name of the property that you found it in
var arrs = {
'one': ['foo', 'bar']
/* two, three etc */
};
for(var i in arrs)
if (arrs[i].indexOf('foo') != -1)
return i;
When you have a group of things, store them in an array (if they are ordered) or an object (if they are named). If you spot a bunch of variables with almost identical names, you're probably doing something wrong.
var things = [
['a','b','c','d'],
['e','f','g','h'],
['i','j','k','l']
];
Then you can loop over them:
for (var i = 0; i < things.length; i++) {
var thing = things[i];
if (thing.indexOf(id) > -1) { // indexOf returns -1 if not found, and the index (starting from 0) if it is.
return i;
}
}
return null;
var arr1 = ['a', 'b', 'c', 'd'];
var arr2 = ['e', 'f', 'g', 'h'];
var arr3 = ['i', 'j', 'k', 'l'];
function chkElem(elem) {
var isFound;
(isFound = arr1).indexOf(elem) > -1 || (isFound = arr2).indexOf(elem) > -1 || (isFound = arr3).indexOf(elem) > -1;
return isFound;
}
alert(chkElem('f'));

Categories

Resources