Page 1 of 1

Sorting a 2-dimensional array

Posted: Tue Sep 07, 2010 1:29 am
by HiddenS3crets
I have an array that follows this structure

Code: Select all

Array (
     [category1]
     (
          [0]
          (
               [title] = A Note
               [timestamp] = 012097521
          )
          [1]
          (
               [title] = Another Note
               [timestamp] = 120974325
          )
     )
     [category2]
     (
          [0]
          (
               [title] = A Third Note
               [timestamp] = 093297534
          )
          [1]
          (
               [title] = A Fourth Note
               [timestamp] = 012190785
          )
     )
)
What would be the easiest way to sort this array by timestamp and store it in a new array?

I have a way to do this but it sorts with respect to the category, i.e., it will sort each array separately by timestamp. I need a way to sort it with no regard to what category it falls under

An example of the outputted, sorted array would look like this:

Code: Select all

Array
(
     [category2]
     (
          [title] = A Fourth Note
          [timestamp] = 182190785
     )
     [category1]
     (
          [title] = Another Note
          [timestamp] = 120974325
     )
     [category2]
     (
          [title] = A Third Note
          [timestamp] = 093297534
     )
     [category1]
     (
          [title] = A Note
          [timestamp] = 088097521
     )
)

Re: Sorting a 2-dimensional array

Posted: Tue Sep 07, 2010 3:49 am
by Christopher
I would copy the data into an array something like the below, and then sort by timestamp:

Code: Select all

Array (
          [0]
          (
               [title] = A Note
               [timestamp] = 012097521
               [category] = category1
          )
          [1]
          (
               [title] = Another Note
               [timestamp] = 120974325
               [category] = category1
          )
          [2]
          (
               [title] = A Third Note
               [timestamp] = 093297534
               [category] = category2
          )
          [3]
          (
               [title] = A Fourth Note
               [timestamp] = 012190785
               [category] = category2
          )
)

Re: Sorting a 2-dimensional array

Posted: Tue Sep 07, 2010 9:50 am
by HiddenS3crets
ok I'll go with that, I wasn't sure if there was some function that I was overlooking that could make the process more simple

Re: Sorting a 2-dimensional array

Posted: Tue Sep 07, 2010 4:46 pm
by Jonah Bron
The reason you can't do that is because you cannot have multiple indices in an array with the same key.