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.

4
  • show the expected result Commented Jul 11, 2016 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 Commented Jul 11, 2016 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. Commented Jul 11, 2016 at 18:01
  • 1
    explode() will get you the arrays and this question and accepted answer address the sorting you seem to want. Commented Jul 11, 2016 at 18:03

1 Answer 1

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
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.