Custom String Ordering

Asked

Viewed 269 times

2

Hello, I have a problem, I need to sort a set of letters in alphabetical order, but the word order is customized from a specific order of letters.

<?php
     // Pega o conteudo do arquivo texto.
    $arquivo = file_get_contents("texto.txt") or die("Falha ao pega o conteudo!");
    // Pega as palavras do arquivo e guarda cada palavra em uma posição da array.
    $resultado = explode(" ", $arquivo);

    $ordenado = array();

    //array de inicialização do alfabeto booglan
    function cmpA($a,$b){
        $alfabeto_boo = array('t','w','h','z','k','d','f','v','c','j','x','l','r','n','q','m','g','p','s','b');

        if(strcmp(substr($a, 0,1), substr($b, 0,1)) == 0){
            echo "$a:$b<br>";
            return 0;
        }
        return (array_search($a,$alfabeto_boo) < array_search($b,$alfabeto_boo)) ? -1 : 1;
    }

    usort($resultado, "cmpA");
    echo '<pre>' . print_r($resultado,true) . '</pre>';

?>

But with the usort, it’s returning me sorter, but not in the order determined by the proposed alphabet.

  • You can edit the question you asked earlier, than create another, if the purpose is the same. Then it goes to reopening

  • I tried, but it didn’t roll. I don’t know what happened, I’m also new around here.

  • About the purpose, they are not equal, but have the best idea, in the other topic, I already manage to solve.

1 answer

0


I created a script, based on yours, which will solve your problem. The code is commented.

<?php 

$resultado  = array("wtps", "tdtt", "tktf"); // aqui é o resultado do explode
$posicoes = array();

echo '<pre>' . print_r($resultado ,true) . '</pre>';

/*

A função abaixo identifica as posições de cada caracter de cada string analisada

*/

function identificaPosicoes(&$string){
    $quantidadeDeCarcteres = 16; // aqui você define o máximo de caracteres que cada string terá
    // coloquei 16 pois acho que é suficiente
    $alfabeto_boo = array('t','w','h','z','k','d','f','v','c','j','x','l','r','n','q','m','g','p','s','b');
    $stringPos = array();
    for($x = 0; $x < $quantidadeDeCarcteres; $x++){
        if(isset($string[$x])){
            for($y = 0; $y < count($alfabeto_boo); $y++){
                if($string[$x] == $alfabeto_boo[$y]){
                    $stringPos[] = $y;
                }
            }
        } else {
            $stringPos[] = 0;
        }
    }
    return $stringPos;
}

$posicoes = array_map("identificaPosicoes",$resultado); // aqui ele analisa as posições e retona tudo para um array
asort($posicoes); // aqui ele ordena o array sem alterar o valor das chaves

$ordenados = array();
foreach($posicoes as $chave => $valor){
    $ordenados[] = $resultado[$chave]; // aqui ele pega o valor das chaves na ordem para ordenar o resultado
}

echo '<pre>' . print_r($ordenados,true) . '</pre>'; // aqui ele gera o resultado

using your array:

array("nqkrm", "dbpvxzj", "bsthm", "cjwdc", "qflzwj", "mxnr", "xbzj", "pspb", "kcwr", "trf", "csw", "frqfs", "mmkrjpcz");

The result was this:

Array
(
    [0] => trf
    [1] => kcwr
    [2] => dbpvxzj
    [3] => frqfs
    [4] => cjwdc
    [5] => csw
    [6] => xbzj
    [7] => nqkrm
    [8] => qflzwj
    [9] => mxnr
    [10] => mmkrjpcz
    [11] => pspb
    [12] => bsthm
)
  • Opa, então, não existe as letras A, E, I, O, U. &#xA;As letras são assim: nqkrm dbpvxzj bsthm cjwdc qflzwj mxnr xbzj pspb kcwr trf csw frqfs mmkrjpcz rvtfn ztcbglxj bnpbjz spmcmpls bdlr fwscncn wksx tnxn hpmwbs ftcxbv whqn. Funny, with the way you flew, it really worked.

  • @Luizmiguel has another solution... I’ll edit it. Wait there.

  • @Luizmiguel putting that if before the ULTIMO Return, any character or letter that does not exist in your array will return -1. Then you will go to the end and not to the beginning.

  • @Luizmiguel there is no vowel in the text?

  • So, problem that he is not ordering right. I think it is some logical problem, q I did not notice. How is the result coming: Array ( [0] => hnh [1] => xdrbsmwn [2] => kxp [3] => dmddhsk [4] => hzlvdfhw .... }

  • There is no vowel in the text

  • @Luizmiguel The problem is in your array. I will edit the answer and see if it works.

  • @Luizmiguel try to use Trim to erase spacing like I did.

  • @Luizmiguel did other tests and is coming wrong even. I will research and if I can help I post a new answer.

  • @Luizmiguel I think I got what you want.

  • @Luizmiguel worked out?

  • Andrei, thank you so much indeed, I get using what you shared with me!

  • @Luizmiguel if you think the answer was helpful, please evaluate

  • 1

    I evaluated, but it appeared that with user with less than 15 of reputation, does not show the score. But I evaluated yes. PS am new here at stackoverflow! Thank you again.

  • @Luizmiguel tranquil. = ) Hug!

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.