How to Sort Json Array Elements in Descending Order

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



Leave a reply



Submit