0

I have a text file I'd like to sort, but I'd like to come up with different sortings. Here's an example of how the file looks:

Joe:100:50/Jane:120:60/Paul:200:34/

So what I'd like to be able to do is turn this into an array and then sort by some of the inner numbers. Normal sort would just put them alphabetical (since their names are first), but I'd like it to sort by all 3 values.

Shira
  • 6,392
  • 2
  • 25
  • 27
Robby Mulvany
  • 59
  • 1
  • 3
  • show the expected result – RomanPerekhrest Jul 11 '16 at 17:57
  • Paul:200:34, Jane:120:60, Joe:100:50 would be the desired result if I'm sorting by the first number – Robby Mulvany Jul 11 '16 at 17:58
  • First split on '/', then split on ':', then sort on the second array item. Try that; it's okay to get errors. Put the code you tried and the errors you got in your question. – bloodyKnuckles Jul 11 '16 at 18:01
  • 1
    [explode()](http://php.net/manual/en/function.explode.php) will get you the arrays and [this question and accepted answer](http://stackoverflow.com/questions/2426917/how-do-i-sort-a-multidimensional-array-by-one-of-the-fields-of-the-inner-array-i) address the sorting you seem to want. – Patrick Q Jul 11 '16 at 18:03

1 Answers1

1

After you've parsed it into an array of arrays, use usort() to sort it by any criteria:

usort($items, function ($a, $b) {
    // compare $a and $b and return 1, 0 or -1
});
Shira
  • 6,392
  • 2
  • 25
  • 27