Array accending and descending javascript [duplicate] - javascript

This question already has answers here:
Using Javascript to sort an array of numeric arrays
(5 answers)
Closed 9 months ago.
Create a program that will allow you to enter a number of integers to input. Then input values for those integers and store them into an array(Array A). After that, create another array(Array B) that will store the doubled value of elements from the first array. Display the two arrays. Display also the ascending order of the first array and the descending order of the second array.

look into array mapping (and arrow functions to make life easier)
e.g. var newNumbers = numbers.map(n => n*2)
Will return another array with all the elements of the numbers array doubled.
also look into sort() and reverse()
e.g. newNumbers.sort(function(a, b){return a-b}).reverse();
Will sort the array and reverse its order.
You need this comparison thing in the sort() because of they way numbers are processed with sort()

Related

How to sort an array of arrays by a specified element? [duplicate]

This question already has answers here:
How to sort 2 dimensional array by column value?
(14 answers)
Closed 4 years ago.
I have an array of arrays, each array within the array is set up like this
[1, firstname, lastname, 22]
I have a total of 12 of these, I am trying to sort the arrays by the 4th element of each array in descending order but nothing I have tried has worked, namely I have been trying to do array.sort with a comparison function and having look at the 4th element in each array but cant get it to work. Here is what I currently have:
function sortArray (a, b){
return a-b;
}
for (var k = 0; k<employeeArray.length; k++){
var j = k-1;
employeeArray.sort(sortArray([k][3],[j][3]));
}
What I was expecting was for the function to sort the arrays by the 4th element but it keeps throwing up Uncaught Type errors.
I would recommend reading up on how sort works.
The .sort method handles the sorting. You do not need a for-loop.
Instead, you need to make your function sort two entries properly:
function sortTwoArrays(a, b){
return a[3] - b[3];
}
Now you can just pass this as the parameter to the .sort method of your employeeArray:
employeeArray.sort(sortTwoArrays);
which will modify the array.

JavaScript Object Array Sorting With Mixed ASC and DESC on Object Values [duplicate]

This question already has answers here:
How to sort an array of objects by multiple fields?
(38 answers)
Closed 4 years ago.
Say for example I have an array of objects with the following basic structure:
{
value1 : "VALUE"
, value2 : "VALUE"
, value3 : "VALUE"
}
Is there a way to sort this array in vanilla JavaScript where it sorts ascending on value1 and value2, but descending on value3? This is just a example of the kind of mixed sort I'm looking at. Pretty anywhere where sorting needs to be done on more than one object value, and it isn't only ascending or descending, but both.
For a bit of reference, I'm trying to build a results list and grant the user the ability to choose which values they want to sort on, and whether those values are sorted ascending or descending prior to being returned to the user.
Sure, when you specify how to sort by implementing the comparison function that is passed to the sort() function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
You can define a compare function for each of the sorting options and simply pass it into the sort() when it's time to sort the array.

Order array of objects by date [duplicate]

This question already has answers here:
How to sort an object array by date property?
(25 answers)
Closed 6 years ago.
for example
myArr= [
{name:"Joe", id:3, date: "2012.10.12"},
{name:"Ed", id:43, date: "2012.02.12"},
{name:"Mark", id:22, date: "2012.02.11"}
];
so how can I sort this Array by date?
This is just small example, but it will be like 1000 Objects inside that Array.
I have searched on the Internet and found some examples that used the sort() function but it doesn't work in my big Array.
Assuming the dates are just strings formatted as in your code, you can do this:
myArr.sort( (a,b) => a.date.localeCompare(b.date) )
The sort method takes as a parameter a function that will be called every time it needs to compare two elements of the array. So to sort by a particular field, you pass a function that compares those fields of the two objects passed in.
The sort comparator function has to return a special value indicating the correct order: -1 if the first parameter (conventionally called a) should come before the second (b); 1 if b should come before a; or 0 if they're equal (so the order doesn't matter). Fortunately, there's already a method that compares strings and returns the proper value for sort (if you call it on a and pass b as a parameter): localeCompare. Since the fields you're comparing are strings, you can just call that on the fields in your comparison function to return the proper value for the sort.

JavaScript pass new array in argument [duplicate]

This question already has answers here:
How to get subarray from array?
(5 answers)
Closed 7 years ago.
Is there a way (like we have in C# generics/linq list.take...) that can take a range of elements of an array in the argument while calling a function, without having to create a new array?
//a simple array with some elements
myArray;
//just to show what I mean...pass the first five elemtns of array to the function
doSomethingWithArray(myArray[0,4]);
function doSomethingWithArray(items) {
//do stuff
}
Sounds like you might be looking for slice.
doSomethingWithArray(myArray.slice(0, 4))
Slice takes start and end parameters and returns a shallow copy of the items in the array that fall within that range. If you want to mutate the array you can consider splice.
Note that the end index is non-inclusive, i.e. myArray.slice(0,4), in the example, returns only elements in the range [0 .. 3].

Sorting objects inside an array [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 8 years ago.
I have an array of objects, and i am basically breaking my head on sorting it,my array is something like dis :
var myArray = [{1:3},{3:19},{5:53},{6:26},{e:53},{c:107},{B: 2},{f: 5}];
But i have to sort this in such a way that my final output is something like this:
myArray = [{c:107},{5:53},{e:53},{6:26},{3:19},{f: 5},{1:3},{B: 2}];
i.e.; based on the value in each object of array element, the array should sorted in descending order.
Thank you in advance.
You can simply use a comparator function in Array.prototype.sort like this
console.log(myArray.sort(function(first, second) {
return second[Object.keys(second)[0]] - first[Object.keys(first)[0]];
}));
Since the key will be different in every object, we get the actual list of keys and taking only the first item in it.

Categories