Viewed   222 times

I'm currently trying to sort a multidimensional array by its subvalues. The structure of the array is:

[0] => Array
    (
        [id] => 87
        [sold] => 50
        [stock] => 991
        [speed] => 1.5
        [days_left] => 660.66666666667
    )

[1] => Array
    (
        [id] => 97
        [sold] => 20
        [stock] => 120
        [speed] => 1.2
        [days_left] => 100
    )

[2] => Array
    (
        [id] => 36
        [sold] => 2
        [stock] => 1020
        [speed] => 1.02
        [days_left] => 1000
    )

The code I'm using is:

usort($data, function($a, $b) { return $a[$_GET['sortby']] - $b[$_GET['sortby']]; });

where the $_GET['sortby'] variable equals the key.

So far so good, everthing is working, it sorts all values correctly EXCEPT the speed! First, I thought it has something to do with the decimal numbers, but the days_left include also decimals and are sorted correctly.. :/

Correct output (days_left):

[0] => Array
    (
        [id] => 97
        [sold] => 20
        [stock] => 120
        [speed] => 1.2
        [days_left] => 100
    )

[1] => Array
    (
        [id] => 87
        [sold] => 50
        [stock] => 991
        [speed] => 1.5
        [days_left] => 660.66666666667
    )

[2] => Array
    (
        [id] => 36
        [sold] => 2
        [stock] => 1020
        [speed] => 1.02
        [days_left] => 1000
    )

Wrong output (speed):

[0] => Array
    (
        [id] => 97
        [sold] => 20
        [stock] => 120
        [speed] => 1.2
        [days_left] => 100
    )

[1] => Array
    (
        [id] => 87
        [sold] => 50
        [stock] => 991
        [speed] => 1.5
        [days_left] => 660.66666666667
    )

[2] => Array
    (
        [id] => 36
        [sold] => 2
        [stock] => 1020
        [speed] => 1.02
        [days_left] => 1000
    )

Hope anybody can help me!

 Answers

3

See usort docs. Float result will be converted to integer. For correct work use this code:

usort(
    $data, 
    function($a, $b) {
        $result = 0;
        if ($a[$_GET['sortby']] > $b[$_GET['sortby']]) {
            $result = 1;
        } else if ($a[$_GET['sortby']] < $b[$_GET['sortby']]) {
            $result = -1;
        }
        return $result; 
    }
);
Friday, November 4, 2022
1
function cmp($a, $b)
{

$sizes = array(
"XXS" => 0,
"XS" => 1,
"S" => 2,
"M" => 3,
"L" => 4,
"XL" => 5,
"XXL" => 6
);

$asize = $sizes[$a];
$bsize = $sizes[$b];

if ($asize == $bsize) {
    return 0;
}

return ($asize > $bsize) ? 1 : -1;
}

usort($your_array, "cmp");
Wednesday, August 24, 2022
 
enrique
 
3

Use multi-byte string functions. There is a function called strcoll which seems to suit your needs.

More info:

  • On how to sort an array of UTF-8 strings
  • How to sort an array of UTF-8 strings?

EDIT: added Peter's working code, below

setlocale(LC_COLLATE, 'sk_SK.utf8');

usort($fb_friends['data'], 'custom_sort');

function custom_sort($a, $b) {
    return strcoll ($a['last_name'], $b['last_name']);
}

foreach ($fb_friends['data'] as $friend) {
    echo '<br>';
    echo $friend['name'];
}
Wednesday, August 3, 2022
 
3

Resolved this issue as follows. I lost track of an helpful article that suggested these steps.

  1. on RowDataBound of GridView, I added Class attribute to the desired column

    protected void gvExcel_RowDataBound(object sender, GridViewRowEventArgs e) {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[22].Attributes.Add("class", "cost");
        }
    } 
    
  2. And made some minor changes to the code as below. Well, the issue is resolved for now.

        try
        {
            DataSet ds = new DataSet();
            ds = businesscase.services.Actuals.GetActualsGridData(bcid, cmd);
            using (System.IO.StringWriter sw = new System.IO.StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    // instantiate a datagrid 
                    GridView gvExcel = new GridView();
                    gvExcel.RowDataBound += new GridViewRowEventHandler(this.gvExcel_RowDataBound);
    
                    gvExcel.DataSource = ds.Tables[0];
                    gvExcel.DataBind();
                    gvExcel.RenderControl(htw);
    
                    response.Write("<style> .cost{mso-number-format:"\#\#0\.00";} </style>");
    
                    //response.Write(style);
                    response.Write(sw.ToString());
                    response.End();
                }
            }
        }
        catch
        {
    
        }
    
Wednesday, November 30, 2022
 
5

Yes, you can write:

int x = 100 - 0x100 + 0100;

That mixes decimal with hex and octal. The values are all converted to binary anyway before the calculation occurs (and the compiler will do the calculation in this example; it won't be evaluated at runtime). And any of the constants can be replaced by an int value that was assigned the appropriate value:

int d = 100;
int h = 0x100;
int o = 0100;
int x = d + h + o;
Monday, October 17, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :