Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort array by cost and name

  {
m: "1",
total: "6",
r: [
{
job_id: "472",
category: "plumbing",
job_cost: "350",
posted_on: "17 Dec 2015",
completed_on: "17 Dec 2015",
job_final_status: "1"
},
{
job_id: "459",
category: "electrical",
job_cost: "600",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "457",
category: "electrical",
job_cost: "1000",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "456",
category: "carpentry",
job_cost: "350",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "454",
category: "electrical",
job_cost: "450",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "433",
category: "plumbing",
job_cost: "400",
posted_on: "15 Dec 2015",
completed_on: "15 Dec 2015",
job_final_status: "1"
}
]
}

Above is response of my web-service. I want array ('r') result like, firstly sort by category name, and if category name are same, then result for that category should be sorted by job_cost higher to lower.

So my desired result is as below:

{
m: "1",
total: "6",
r: [
{
job_id: "456",
category: "carpentry",
job_cost: "350",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "457",
category: "electrical",
job_cost: "1000",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "459",
category: "electrical",
job_cost: "600",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "454",
category: "electrical",
job_cost: "450",
posted_on: "16 Dec 2015",
completed_on: "16 Dec 2015",
job_final_status: "1"
},
{
job_id: "433",
category: "plumbing",
job_cost: "400",
posted_on: "15 Dec 2015",
completed_on: "15 Dec 2015",
job_final_status: "1"
},
{
job_id: "472",
category: "plumbing",
job_cost: "350",
posted_on: "17 Dec 2015",
completed_on: "17 Dec 2015",
job_final_status: "1"
}
]
}
like image 670
Himanshu Upadhyay Avatar asked Apr 16 '26 11:04

Himanshu Upadhyay


2 Answers

This is how I would solve your problem.

First you decode the info with:

 $r = json_decode($yourJSONstring);

Then you can make an empty array that you will fill with occurring categories:

$occurring = array();

then you can combine the arrays with the same category by looping them like so:

foreach ($r as $item) {
    if (isset($occurring [$item['category']])) {
        $occurring [$item['category']][] = $item;
    } else {
        $occurring [$item['category']] = array();
        $occurring [$item['category']][] = $item;
    }
}

Then you can sort those like this:

foreach ($occurring as $key => $item) {
    $occurring[$key] = Classname::array_sort_by_column($item,"job_cost",SORT_DESC);
}

With a function like this:

static public function sort_by_column($sortArray, $columnName, $sortDirection = SORT_ASC) {
    $sortCol = array();

    foreach ($sortArray as $key => $row) {
      $sortCol [$key] = $row[$columnName];
    }

    array_multisort($sortCol , $sortDirection, $sortArray);

    return $sortArray;
}

I Hope this solves your problem :)

like image 175
cpalinckx Avatar answered Apr 18 '26 23:04

cpalinckx


First, you fire a MYSQL query for sort by FILED after that you should use the array sort by value accordingly uasort() function. Try these, may be it will be help you..

like image 40
Vijay Chauhan Avatar answered Apr 19 '26 00:04

Vijay Chauhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!