0

Hi,

I have the below array (or is it called string?) with javascript date and a value. I need to sord the elements by the date but i dont know how to do this?!

[1366754400000, 8], [1366840800000, 3], [1366927200000, 1], [1368482400000, 1], [1384383600000, 1], [1369951200000, 1], [1377554400000, 1], [1377813600000, 8], [1380232800000, 4], [1381960800000, 4], [1382914800000, 6], [1384297200000, 6], [1384383600000, 1], [1386716400000, 2], [1389049200000, 5], [1392764400000, 3], [1393196400000, 1], [1397599200000, 4], [1398636000000, 2], [1401141600000, 1], [1401746400000, 1], [1409954400000, 1], [1410386400000, 5], [1371420000000, 25], [1377122400000, 1], [1386889200000, 1],

any ideas?

/Adam

4
Contributors
6
Replies
44
Views
2 Years
Discussion Span
Last Post by broj1
0

What is your ultimate goal besides sorting this array? What do you want to do with the array? If you want to sort from lowest to highets use: sort() Docs

0

Thanks for your input, my goal is to use it in highchart chart (highstock) and the date field needs to be sorted from oldest to newest.

I tried the sort function but the errormessage confirmed that it indeed is not an array but a string.

Any other ideas?

Cheers
/Adam

1

Well you are getting that error because, your are using a javscript sting inside the sort function.. The sort function only excepts arrays.

array(value, value, value, value);

Now that we know you are using highcharts your should be using javascript to sort your array and not php. Unless you are going to sort the data before you return the array.

here is a javascript solution to get your array sorted by datetime.

function sortarray(dt, id) {
    if (dt[0] === id[0]) {
        return 0;
    }
    else {
        return (dt[0] < id[0]) ? -1 : 1;
    }
}

var arr = [[1366754400000, 8], [1366840800000, 3], [1366927200000, 1], [1368482400000, 1], [1384383600000, 1], [1369951200000, 1], [1377554400000, 1], [1377813600000, 8], [1380232800000, 4], [1381960800000, 4], [1382914800000, 6], [1384297200000, 6], [1384383600000, 1], [1386716400000, 2], [1389049200000, 5], [1392764400000, 3], [1393196400000, 1], [1397599200000, 4], [1398636000000, 2], [1401141600000, 1], [1401746400000, 1], [1409954400000, 1], [1410386400000, 5], [1371420000000, 25], [1377122400000, 1], [1386889200000, 1]];

var sorted = arr.sort(sortarray); 

alert(sorted);
return false;

You can see I took your values and placed them inside an array.

1

If you want to sort using php then you have to:

  1. convert javascript array (of arrays) into php array using json_decode function
  2. sort the php array using array_multisort function

This is the code, see also comments in the code:

// this is original string representing a javascript array of arrays (note square brackets arround your string)
$jsonString = '[[1366754400000, 8], [1366840800000, 3], [1366927200000, 1], [1368482400000, 1], [1384383600000, 1], [1369951200000, 1], [1377554400000, 1], [1377813600000, 8], [1380232800000, 4], [1381960800000, 4], [1382914800000, 6], [1384297200000, 6], [1384383600000, 1], [1386716400000, 2], [1389049200000, 5], [1392764400000, 3], [1393196400000, 1], [1397599200000, 4], [1398636000000, 2], [1401141600000, 1], [1401746400000, 1], [1409954400000, 1], [1410386400000, 5], [1371420000000, 25], [1377122400000, 1], [1386889200000, 1]]';
// convert json into php array
$arr = json_decode($jsonString);
// prepare temporary arrays for sorting
foreach($arr as $key => $val) {
    $times[$key] = $val[0];
    $values[$key] = $val[1];
}   
// sort the array using temporary arrays
array_multisort($times, SORT_ASC, $values, SORT_ASC, $arr);
// check the sorted array
echo '<pre>' . print_r($arr, 1) . '</pre>';

You can convert sorted php array back to javascript using json_encode function so you can use it in your javascript library.

Edited by broj1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.