how to sort the json array by keys in descending order using angular
You can extract the "keys" of your array using the Object.keys() function, then use the sort function to sort that array of keys, and generate a new object iterating over each keys there... Code would be (keeping arr as the name of your array):
// Get the keys of your "array"
let arrayOfKeys = Object.keys(arr)
// We sort it out...
let sortedArray = arrayOfKeys.sort()
// We will create the "final" array
let resultingArray = {}
// Now we iterate over each element, in order:
sortedArray.forEach((element) => {
resultingArray[element] = arr[element]
})
...of course this can be shortened wildly, for example:
var resultingArr = {}
Object.keys(arr).sort().forEach(e -> { resultingArr[e] = arr[e] })
EDIT: I see that you need to sort the array in descending order. This can be done using a custom sort function, or using the function reverse(). Just correct the sorting step:
// We sort it out...
let sortedArray = arrayOfKeys.sort().reverse()
PHP - Sort json array descending order
Just simply reverse the sort function so that you are doing b-a
instead of a-b
.
function sortDate($a, $b) {
return strtotime($b['Date']) - strtotime($a['Date']);
}
how to sort json array with dates in descending order with php
You can try this way with pushing all the individual array's to a single array and the use array_multisort
like below to sort by date field,
$files = glob('data/*.json'); // all json files in data dir
foreach($files as $file) {
$main[] = json_decode(file_get_contents($file),1); // push individual array
}
array_multisort(array_column($main, 'date'), SORT_DESC, $main);
print_r($main);
Sorting an array of objects by property values
Sort homes by price in ascending order:
homes.sort(function(a, b) {
return parseFloat(a.price) - parseFloat(b.price);
});
Or after ES6 version:
homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
Some documentation can be found here.
For descending order, you may use
homes.sort((a, b) => parseFloat(b.price) - parseFloat(a.price));
Sorting an array of JSON objects in ascending order in Laravel Response
In your sortBy() remove second parameter:
collect($life)->sortBy('days_left');
// automatically sorting in ascending order.
If you sort in descending order then use:
collect($life)->sortByDesc('days_left');
I hope it would be helpful.
JAVA - Sort Array of Json by multiple values while preserving previous sort order
A sorting algorithm that preserves the existing order on same-valued entries is called stable. In Java, you can consult the API whether a given sorting function is guaranteed to be stable, such as Arrays.sort.
The typical way of sorting using multiple keys is to sort the entries sequentially with a stable sorting algorithm in reverse order of keys.
For example, if you want to order by first name first and last name second, you would first sort by last name and then by first name.
You also need to make sure that the data structure you use preserves the order of insertion, for example a Set or Map may not preserve that.
Related Topics
Display Image from Http Response With Image Content Type
Prevent JavaScript String That Contains a Number from Dropping Zeros
Getting Typeerror: This.Props Is Undefined on Reactjs
Angular 2 Not Updating Until Any Object Is Clicked
How to Set Profile Image as First Letters of First and Last Name
I Want to Change the Page Background Color, But It Doesn't Work as I Expect in Chrome
How to Check If Character Is a Letter in JavaScript
Will a React Component Re-Render If Its Props Are Updated, But Don't Change in Value
How to Hide the Android Keyboard Using JavaScript
Res.Sendfile in Node Express With Passing Data Along
How to Hide and Show Div by Id Based on the Value of Selected Drop Down -Jquery and JavaScript
How to Access Variables from Another File
React Display Line Breaks from Saved Textarea
How to Get List of All Timezones in JavaScript
How to Add Space Between Every 4 Characters in JavaScript
How to Handle Multiple Submit Buttons in a Form Using Angular Js
Jquery Validation Plugin - Typeerror: $(...).Validate Is Not a Function
Looping Through Dynamically Generated Checkboxes to Get Values