Order array of objects by date [duplicate] - javascript

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.

Related

Array accending and descending javascript [duplicate]

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()

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.

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].

How the sorting method of integer array works ? JS [duplicate]

This question already has answers here:
How does Javascript's sort() work?
(8 answers)
Closed 8 years ago.
var anArray = [ 5, 4, 8 , 1, 3] ;
anArray.sort(function (a,b){return a - b});
1) Can someone run me through how JavaScript will execute the sort method with function that is passed as a parameter ?
It will compare 5 with 4 then because it's positive, 4 will be before 5. Then it will compare 5 with every other number but 1 and 3 are also smaller than 5. So how java script know which position to put them before 5?
then it will compare 4 with every other number and 8 with every other number etc...
how java script do this? I want to do it with pen and paper.
2) why the function that is passed as an parameter is nameless ?
thank you.
Exactly how the comparator function will be called — that is, the sequence of values passed in — is not defined by the specification of the language. It completely depends on the particular JavaScript implementation and (probably) on the values in the array being sorted. Suffice to say that the sorting algorithm calls your function when it wants to compare two numbers, and that's that.The function is expected to return a value that's negative, zero, or positive, indicating that the ordering of the two numbers should be that the first one comes first, that either can come first, or that the second one should come first. A quick way to do that is to just subtract the second number from the first.
The function in your sample code is an anonymous function. It needs no name because it will be bound to a symbol in the receiving function as a result of the function call itself. You can give the function a name if you want to.

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