Z
Z
Zakhar Storozhuk2017-03-14 11:47:54
PHP
Zakhar Storozhuk, 2017-03-14 11:47:54

How to sort rows by a specific key?

Such a situation, I'm not strong in algorithms, I need to implement this:
There are lines, you need to sort them by a certain parameter from these lines.
For the input to be:

19:16:30-13.03.17 | Login: Gerard_Muscle
19:16:31-13.03.17 | Login: Hotaru_Nagiwaro
19:16:48-13.03.17 | Login: Gerard_Muscle
19:16:48-13.03.17 | Login: Santa_Moroz
19:17:05-13.03.17 | Login: Gerard_Muscle

And at the output:
19:16:30-13.03.17 | Login: Gerard_Muscle
19:16:48-13.03.17 | Login: Gerard_Muscle
19:17:05-13.03.17 | Login: Gerard_Muscle
19:16:31-13.03.17 | Login: Hotaru_Nagiwaro
19:16:48-13.03.17 | Login: Santa_Moroz

Tell me where to dig.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-03-14
@Phell

we create an array explode("|", $string);and sort it, I don’t know what kind of lines you have, but I think I’ll convey the essence:

$arr = [];
foreach($lines as $one){
$arr_str =  explode("|", $string);
$arr[$arr_str[1]] = $arr_str[0];
}
ksort($arr);
foreach($arr as $key=>$one){
echo $one. '|' .$key;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question