I have an array of object which created manually by be and I need to sort it in desc or asc order based on date.
First I have make it a collection, using collect function in php laravel.
I already tried sortBy and sortby desc but it does not work:
[
{
"id": 22,
"date": "2022-03-24",
"form_name": "cashbook",
"description": "sed<br>asdas",
"credit": 90,
"debit": 0,
"status": 1,
"balance": 90
},
{
"id": 28,
"date": "2022-03-23",
"form_name": "cashbook",
"description": "sed<br>Check the To Do List Task if task due date/time is passed",
"credit": 700,
"debit": 0,
"status": 1,
"balance": 790
},
{
"id": 29,
"date": "2022-03-24",
"form_name": "cashbook",
"description": "sed<br>نقد رقم",
"credit": 700,
"debit": 0,
"status": 1,
"balance": 1490
},
{
"id": 30,
"date": "2022-03-24",
"form_name": "cashbook",
"description": "sed<br>نقد رقم",
"credit": 0,
"debit": 700,
"status": 1,
"balance": 790
},
{
"id": 31,
"date": "2022-03-24",
"form_name": "cashbook",
"description": "sed<br>نقد رقم",
"credit": 700,
"debit": 0,
"status": 1,
"balance": 1490
},
{
"id": 14,
"date": "2022-03-21",
"form_name": "advance",
"description": "sed<br>2000",
"credit": 9090,
"debit": 0,
"status": 1,
"balance": 10580
},
{
"id": 18,
"date": "2022-03-21",
"form_name": "advance",
"description": "sed<br>2000",
"credit": 9090,
"debit": 0,
"status": 1,
"balance": 19670
},
{
"id": 98,
"date": "2022-03-25",
"form_name": "purchase",
"description": "شلجم--توڑا--4<span class=\"backslash\">/</span>23--@lang(\"dashboard.vehicle_number\")--2532 LAS",
"credit": 0,
"debit": -44,
"status": 1,
"balance": 19626
},
{
"id": 99,
"date": "2022-03-25",
"form_name": "purchase",
"description": "sed<br>کدو--توڑا--3<span class=\"backslash\">/</span>9--@lang(\"dashboard.vehicle_number\")--2532 LAS",
"credit": 335,
"debit": 0,
"status": 1,
"balance": 19961
},
{
"id": 100,
"date": "2022-03-25",
"form_name": "purchase",
"description": "sed<br>کدو--توڑا--3<span class=\"backslash\">/</span>9--@lang(\"dashboard.vehicle_number\")--2532 LAS",
"credit": 335,
"debit": 0,
"status": 1,
"balance": 20296
},
{
"id": 101,
"date": "2022-03-25",
"form_name": "purchase",
"description": "sed<br>کدو--توڑا--90<span class=\"backslash\">/</span>90--@lang(\"dashboard.vehicle_number\")--2532 LAS",
"credit": 18711,
"debit": 0,
"status": 1,
"balance": 39007
},
{
"id": 102,
"date": "2022-03-25",
"form_name": "purchase",
"description": "sed<br>کدو--توڑا--90<span class=\"backslash\">/</span>90--@lang(\"dashboard.vehicle_number\")--2532 LAS",
"credit": 18891,
"debit": 0,
"status": 1,
"balance": 57898
},
{
"id": 103,
"date": "2022-03-25",
"form_name": "purchase",
"description": "sed<br>شلجم--توڑا--90<span class=\"backslash\">/</span>90--@lang(\"dashboard.vehicle_number\")--2532 LAS",
"credit": 9459,
"debit": 0,
"status": 1,
"balance": 67357
},
{
"id": 104,
"date": "2022-03-28",
"form_name": "purchase",
"description": "sed<br>شلجم--توڑا--50<span class=\"backslash\">/</span>90--@lang(\"dashboard.vehicle_number\")--abc",
"credit": 5157,
"debit": 0,
"status": 1,
"balance": 72514
},
{
"id": 105,
"date": "2022-03-28",
"form_name": "purchase",
"description": "sed<br>شلجم--توڑا--50<span class=\"backslash\">/</span>90--@lang(\"dashboard.vehicle_number\")--abc123",
"credit": 5355,
"debit": 0,
"status": 1,
"balance": 77869
},
{
"id": 106,
"date": "2022-03-28",
"form_name": "purchase",
"description": "کدو--توڑا--90<span class=\"backslash\">/</span>90",
"credit": 18891,
"debit": 0,
"status": 1,
"balance": 96760
},
{
"id": 107,
"date": "2022-03-28",
"form_name": "purchase",
"description": "کدو--توڑا--50<span class=\"backslash\">/</span>23",
"credit": 6442,
"debit": 0,
"status": 1,
"balance": 103202
},
{
"id": 108,
"date": "2022-03-28",
"form_name": "purchase",
"description": "sed<br>کدو--پیٹی--4<span class=\"backslash\">/</span>23",
"credit": 155,
"debit": 0,
"status": 1,
"balance": 103357
},
{
"id": 109,
"date": "2022-04-02",
"form_name": "purchase",
"description": "sed<br>کدو--توڑا--90<span class=\"backslash\">/</span>90--@lang(\"dashboard.vehicle_number\")--90",
"credit": 18508,
"debit": 0,
"status": 1,
"balance": 121865
},
{
"id": 110,
"date": "2022-04-25",
"form_name": "purchase",
"description": "sed<br>کدو--توڑا--90<span class=\"backslash\">/</span>180",
"credit": 27292,
"debit": 0,
"status": 1,
"balance": 149157
},
{
"id": 116,
"date": "2022-04-28",
"form_name": "purchase",
"description": "sed<br>کدو--توڑا--90<span class=\"backslash\">/</span>23--@lang(\"dashboard.vehicle_number\")--mns-123",
"credit": 11595,
"debit": 0,
"status": 1,
"balance": 160752
},
{
"id": 117,
"date": "2022-04-28",
"form_name": "purchase",
"description": "sed<br>کدو--توڑا--90<span class=\"backslash\">/</span>23",
"credit": 11595,
"debit": 0,
"status": 1,
"balance": 172347
},
{
"id": 119,
"date": "2022-04-28",
"form_name": "purchase",
"description": "sed<br>کدو--توڑا--90<span class=\"backslash\">/</span>23",
"credit": 0,
"debit": -7455,
"status": 1,
"balance": 164892
},
{
"id": 120,
"date": "2022-07-21",
"form_name": "purchase",
"description": "sed<br>کدو--توڑا--من--100<span class=\"backslash\">/</span>50",
"credit": 384,
"debit": 0,
"status": 1,
"balance": 165276
},
{
"id": 121,
"date": "2022-07-22",
"form_name": "purchase",
"description": "sed<br>کدو--توڑا--من--50<span class=\"backslash\">/</span>150",
"credit": 219,
"debit": 0,
"status": 1,
"balance": 165495
},
{
"id": 124,
"date": "2022-07-22",
"form_name": "purchase",
"description": "sed<br>کدو--توڑا--من--80<span class=\"backslash\">/</span>120",
"credit": 492,
"debit": 0,
"status": 1,
"balance": 165987
},
{
"id": 127,
"date": "2022-07-22",
"form_name": "purchase",
"description": "sed<br>کدو--توڑا--50<span class=\"backslash\">/</span>100",
"credit": 11100,
"debit": 0,
"status": 1,
"balance": 177087
},
{
"id": 101,
"date": "2022-03-25",
"form_name": "purchase",
"description": "@lang(\"dashboard.buyer\")--jkj",
"credit": "90",
"debit": 0,
"status": 1,
"balance": 177177
},
{
"id": 108,
"date": "2022-03-28",
"form_name": "purchase",
"description": "@lang(\"dashboard.buyer\")--مزدوری",
"credit": "90",
"debit": 0,
"status": 1,
"balance": 177267
},
{
"id": 108,
"date": "2022-03-28",
"form_name": "purchase",
"description": "@lang(\"dashboard.buyer\")--sdsa",
"credit": "90",
"debit": 0,
"status": 1,
"balance": 177357
},
{
"id": 109,
"date": "2022-04-02",
"form_name": "purchase",
"description": "@lang(\"dashboard.buyer\")--opop",
"credit": "9090",
"debit": 0,
"status": 1,
"balance": 186447
},
{
"id": 109,
"date": "2022-04-02",
"form_name": "purchase",
"description": "@lang(\"dashboard.buyer\")--dsadsa",
"credit": "9099",
"debit": 0,
"status": 1,
"balance": 195546
},
{
"id": 98,
"date": "2022-03-25",
"form_name": "purchase",
"description": "@lang(\"dashboard.seller\")--mll",
"credit": "90",
"debit": 0,
"status": 1,
"balance": 195636
},
{
"id": 110,
"date": "2022-04-25",
"form_name": "purchase",
"description": "@lang(\"dashboard.seller\")--مزدوری",
"credit": "500",
"debit": 0,
"status": 1,
"balance": 196136
},
{
"id": 13,
"date": "2022-01-10",
"form_name": "supplies",
"description": "12<span class=\"backslash\">/</span>23--توڑا",
"credit": 0,
"debit": 2072,
"status": 1,
"balance": 194064
},
{
"id": 22,
"date": "2022-02-22",
"form_name": "supplies",
"description": "1<span class=\"backslash\">/</span>23--توڑا",
"credit": 0,
"debit": 87,
"status": 1,
"balance": 193977
},
{
"id": 22,
"date": "2022-02-22",
"form_name": "supplies",
"description": "@lang(\"dashboard.expense_by_customer\")--مزدوری",
"credit": 0,
"debit": 32,
"status": 1,
"balance": 193945
},
{
"id": 54,
"date": "2022-04-02",
"form_name": "loot",
"description": "@lang(\"dashboard.expensebycustomer\")--rent",
"credit": "90",
"debit": 0,
"status": 1,
"balance": 194035
},
{
"id": 54,
"date": "2022-04-02",
"form_name": "loot",
"description": "@lang(\"dashboard.expensebycustomer\")--mlklkl",
"credit": "9090",
"debit": 0,
"status": 1,
"balance": 203125
},
{
"id": 54,
"date": "2022-04-02",
"form_name": "loot",
"description": "@lang(\"dashboard.expensebycustomer\")--jkjkjk",
"credit": "9090",
"debit": 0,
"status": 1,
"balance": 212215
},
{
"id": 13,
"date": "2022-05-10",
"form_name": "loading",
"description": "@lang(\"dashboard.advance_payment\")--مزدوری",
"credit": "90",
"debit": 0,
"status": 1,
"balance": 212305
},
{
"id": 14,
"date": "2022-05-10",
"form_name": "loading",
"description": "@lang(\"dashboard.advance_payment\")--rent",
"credit": "90",
"debit": 0,
"status": 1,
"balance": 212395
},
{
"id": 3,
"date": "2022-05-17",
"form_name": "sale",
"description": "@lang(\"dashboard.add-lootWiseSales\")---کدو--1<span class=\"backslash\">/</span>121<br>کدو--1<span class=\"backslash\">/</span>122<br>",
"credit": 0,
"debit": "226",
"status": 1,
"balance": 212169
},
{
"id": 7,
"date": "2022-05-24",
"form_name": "sale",
"description": "@lang(\"dashboard.add-productWiseSales\")---کدو--4<span class=\"backslash\">/</span>4<br>شلجم--2<span class=\"backslash\">/</span>2<br>",
"credit": 0,
"debit": "542",
"status": 1,
"balance": 211627
},
{
"id": 8,
"date": "2022-05-24",
"form_name": "sale",
"description": "@lang(\"dashboard.add-productWiseSales\")---کدو--1<span class=\"backslash\">/</span>1<br>شلجم--1<span class=\"backslash\">/</span>1<br>",
"credit": 0,
"debit": "188",
"status": 1,
"balance": 211439
},
{
"id": 9,
"date": "2022-05-24",
"form_name": "sale",
"description": "@lang(\"dashboard.add-productWiseSales\")---کدو--4<span class=\"backslash\">/</span>4<br>شلجم--0<span class=\"backslash\">/</span>0<br>",
"credit": 0,
"debit": "433",
"status": 1,
"balance": 211006
},
{
"id": 10,
"date": "2022-05-26",
"form_name": "sale",
"description": "@lang(\"dashboard.add-productWiseSales\")---کدو--1<span class=\"backslash\">/</span>1<br>شلجم--1<span class=\"backslash\">/</span>1<br>",
"credit": 0,
"debit": "231",
"status": 1,
"balance": 210775
},
{
"id": 7,
"date": "2022-07-05",
"form_name": "productIn",
"description": "asa--شاپر--abc--",
"credit": 650,
"debit": 650,
"status": 1,
"balance": 210775
},
{
"id": 9,
"date": "2022-07-26",
"form_name": "productIn",
"description": "شلجم--شاپر--sdsa--کدو--شاپر--jkjjkjk--",
"credit": 8100,
"debit": 0,
"status": 1,
"balance": 218875
},
{
"id": 9,
"date": "2022-07-26",
"form_name": "productIn",
"description": "@lang(\"dashboard.expensebycustomer\")--asas",
"credit": "90",
"debit": 0,
"status": 1,
"balance": 218965
},
{
"id": 18,
"date": "2022-07-05",
"form_name": "productInSale",
"description": "asa--شاپر--شاپر--4<span class=\"backslash\">/</span>12121",
"credit": 0,
"debit": 54350,
"status": 1,
"balance": 164615
},
{
"id": 26,
"date": "2022-07-13",
"form_name": "productInSale",
"description": "مٹر--شاپر--شاپر--20<span class=\"backslash\">/</span>500",
"credit": 0,
"debit": 10240,
"status": 1,
"balance": 154375
},
{
"id": 27,
"date": "2022-07-26",
"form_name": "productInSale",
"description": "شلجم--شاپر--شاپر--10<span class=\"backslash\">/</span>300",
"credit": 0,
"debit": 3080,
"status": 1,
"balance": 151295
},
{
"id": 28,
"date": "2022-07-26",
"form_name": "productInSale",
"description": "کدو--شاپر--شاپر--5<span class=\"backslash\">/</span>1000",
"credit": 0,
"debit": 5275,
"status": 1,
"balance": 146020
}
]
here is my array of object.
CodePudding user response:
This should help: How to sort a JSON array with PHP
Below, $jsonArray is your array of JSON objects.
$data = json_decode($jsonArray, false);
usort($data, function ($a, $b) {
return $a->id <=> $b->id
});
Learn more about usort: https://www.php.net/manual/en/function.usort.php
Learn more about spaceship operator: https://www.tutorialspoint.com/php7/php7_spaceship_operator.htm
Edits suggested by Timo have been made to this post!
CodePudding user response:
$date = array_column($your_array_of_object, 'column name base on for sorting like date etc'); array_multisort($date, SORT_DESC, $your array of object);
this is worked at my side