I have an array:
arr4=["John Alpha","Allen Beta", "Elle Cappa"];
I have a sort function that sorts a string array into descending order
function getSize(arr){
return arr.length;
}
function sortStringArrDes(arr){
arrSize=getSize(arr);
var temp;
for(j=0; j<arrSize-1; j++){
for(i=0; i<arrSize-1; i++){
if (arr[i] < arr[i+1]){
temp = arr[i+1];
arr[i+1] = arr[i];
arr[i] = temp;
}
}
}
return arr;
}
Can I sort the array by surname using the sortStringArrDes or is there another, less plex way to do this?
I have an array:
arr4=["John Alpha","Allen Beta", "Elle Cappa"];
I have a sort function that sorts a string array into descending order
function getSize(arr){
return arr.length;
}
function sortStringArrDes(arr){
arrSize=getSize(arr);
var temp;
for(j=0; j<arrSize-1; j++){
for(i=0; i<arrSize-1; i++){
if (arr[i] < arr[i+1]){
temp = arr[i+1];
arr[i+1] = arr[i];
arr[i] = temp;
}
}
}
return arr;
}
Can I sort the array by surname using the sortStringArrDes or is there another, less plex way to do this?
Share Improve this question edited Feb 25, 2019 at 3:36 B.Uriah asked Feb 25, 2019 at 3:33 B.UriahB.Uriah 475 silver badges11 bronze badges 08 Answers
Reset to default 12Rather than trying to reinvent the wheel, you might consider using the native Array.prototype.sort
method:
const arr4 = ["John Alpha", "Elle Cappa", "Allen Beta"];
arr4.sort((a, b) => b.split(' ')[1].localeCompare(a.split(' ')[1]));
console.log(arr4);
.localeCompare()
returns an integer representing whether the called string or the passed string es first alphabetically.
Here's a one liner
arr4.sort(x => arr4.map(y => y.split(' ')[1]) ).reverse()
Another solution to this problem can be aproached with Array.sort() in conjunction with String.match() and the already mentioned String.localeCompare(). Note this will also work if you have pound surnames.
const input = ["John Alpha", "Elle Cappa", "Allen Beta", "Rob Collyses Chung"];
input.sort(
(a, b) => b.match(/\s(.+)/)[1].localeCompare(a.match(/\s(.+)/)[1])
);
console.log(input);
Even more, you should also note that Array.sort()
will mutate your original array, if you don't want that to happen, you can create a copy of the array before sort it using Array.slice(). Example:
const input = ["John Alpha", "Elle Cappa", "Allen Beta", "Rob Collyses Chung"];
let res = input.slice().sort(
(a, b) => b.match(/\s(.+)/)[1].localeCompare(a.match(/\s(.+)/)[1])
);
console.log("Original is not mutated: ", input);
console.log("Sorted copy: ", res);
You could create a pare function like below and use .sort()
to sort by last name
var names = ["Allen Beta", "John Alpha", "Elle Cappa"];
var sortedNames = names.sort(pare);
console.log(sortedNames);
function pare(a, b) {
var splitA = a.split(" ");
var splitB = b.split(" ");
var lastA = splitA[splitA.length - 1];
var lastB = splitB[splitB.length - 1];
if (lastA < lastB) return -1;
if (lastA > lastB) return 1;
return 0;
}
Another solution is to use two for loops, split each array item, reverse it and sort it. Then reverse it once sorted and join it back together. See code below:
var arr4=["Allen Beta", "John Alpha", "Elle Cappa"];
var arr5 = []; // Keep the original array as is.
for(var i = 0; i < arr4.length; i++){
arr5[i] = arr4[i].split(" ").reverse().join(" ");
}
arr5.sort();
for(var i = 0; i < arr5.length; i++){
arr5[i] = arr5[i].split(" ").reverse().join(" ");
}
console.log(arr5); // a b c
console.log(arr5.reverse()); // c b a
//Try this way
let arr = [`John Alpha`,`Allen Beta`,`Elle Cappa`];
let new_arr=arr.map(item=>{
return item.split(' ');
})
let sorting = new_arr.sort((a,b)=>{
return b[b.length-1].localeCompare(a[a.length-1]);
})
let merging= sorting.map(item=>{
return item.join(' ');
})
console.log(merging)
<!-- language: lang-js -->
let arr = [`John Alpha`,`Allen Beta`,`Elle Cappa`];
var newarr=[];
for(x=0;x<arr.length;x++){
var z=arr[x].split(" ");
var z=z.reverse();
newarr[x]=z;
}
var v=newarr.sort();
var v=v.reverse();
console.log(v);
<!-- end snippet -->
Here I use .split(' ')[1]
to get the lastname for the condition and it looks like it would work.
arr4=["John Alpha","Allen Beta", "Elle Cappa"];
function sortStringArrDes(arr){
arrSize=arr.length;
var temp;
for(j=0; j<arrSize-1; j++){
for(i=0; i<arrSize-1; i++){
if (arr[i].split(' ')[1] < arr[i+1].split(' ')[1]){
temp = arr[i+1];
arr[i+1] = arr[i];
arr[i] = temp;
}
}
}
return arr;
}
console.log(sortStringArrDes(arr4))