Example 1: javascript orderby
var items = [
{ name: 'Edward', value: 21 },
{ name: 'Sharpe', value: 37 },
{ name: 'And', value: 45 },
{ name: 'The', value: -12 },
{ name: 'Magnetic', value: 13 },
{ name: 'Zeros', value: 37 }
];
items.sort(function (a, b) {
return a.value - b.value;
});
items.sort(function(a, b) {
var nameA = a.name.toUpperCase();
var nameB = b.name.toUpperCase();
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
return 0;
});
Example 2: sorting in js
var arr = [23, 34343, 1, 5, 90, 9]
var sortedArr = [];
arr.forEach(x => {
if (sortedArr.length == 0)
sortedArr.push(x)
else {
if (sortedArr[0] > x) sortedArr.unshift(x)
else if (sortedArr[sortedArr.length - 1] < x) sortedArr.push(x)
else sortedArr.splice(sortedArr.filter(y => y < x).length, 0, x)
}
})
console.log(sortedArr);
console.log(arr.sort((a,b)=>{
return a-b;
}));
Example 3: javascript sort
homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
Example 4: array sort php
$room_details = array(
"2020-09-27": [
{
"content": "how are you",
"detail_id": "1",
"time": "17:57:28",
"chat_time": "2020-09-24 17:57:28",
"width": "0",
"height": "0",
"type": "1",
"distance_time": "26 days ago",
"avatar": "uploads/MemberImage/20200922-1436-image-5f699b536f438-0.png",
"position": 2
},
{
"content": "I am fine, thanks",
"detail_id": "2",
"time": "17:57:45",
"chat_time": "2020-09-24 17:57:45",
"width": "0",
"height": "0",
"type": "1",
"distance_time": "26 days ago",
"avatar": "uploads/MemberImage/20200922-1436-image-5f699b536f438-0.png",
"position": 2
},
],
"2020-09-24": [
{
"content": "how are you",
"detail_id": "1",
"time": "17:57:28",
"chat_time": "2020-09-24 17:57:28",
"width": "0",
"height": "0",
"type": "1",
"distance_time": "26 days ago",
"avatar": "uploads/MemberImage/20200922-1436-image-5f699b536f438-0.png",
"position": 2
},
{
"content": "I am fine, thanks",
"detail_id": "2",
"time": "17:57:45",
"chat_time": "2020-09-24 17:57:45",
"width": "0",
"height": "0",
"type": "1",
"distance_time": "26 days ago",
"avatar": "uploads/MemberImage/20200922-1436-image-5f699b536f438-0.png",
"position": 2
},
],
);
sort($room_details);
$room_details = array(
"2020-09-24": [
...
],
"2020-09-27": [
...
],
);
Example 5: php rearrange array
function rearrange_array($array, $key) {
while ($key > 0) {
$temp = array_shift($array);
$array[] = $temp;
$key--;
}
return $array;
}
Example 6: JavaScript Sorting Arrays
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();