2011-04-12 177 views
7

我需要一個函數在PHP中根據任意順序對單詞列表進行排序。PHP按任意順序排序

列表中不是我預定義順序的任何單詞應按字母順序排列在列表的末尾。

以下是我的第一次嘗試,它既不優雅也不高效。你能建議一個更好的方法來實現這個嗎?

感謝

public static function sortWords(&$inputArray){ 
    $order=array("Banana","Orange", "Apple", "Kiwi"); 
    sort($inputArray); 
    for($i=0;$i<count($inputArray));$i++){ 
     $ac = $inputArray[$i]; 
     $position = array_search($ac,$order); 
     if($position !== false && $i != $position){ 
      $temp=$inputArray[$position]; 
      $inputArray[$position]=$inputArray[$i]; 
      $inputArray[$i]=$temp; 
     } 
    } 
} 
+0

您可以先對兩個列表進行排序(NlogN + MlogM時間),然後遍歷匹配列表(N + M時間)。既然你必須排序,這是最佳的。 – bdares 2011-04-12 08:40:04

+0

我知道這對你的問題沒有關係,但是你在'for'語句中有一個錯誤,兩個右括號而不是'$ i AJJ 2011-04-12 08:44:09

回答

1
public static function sortWords($inputArray){ 
    $order=array("Banana","Orange", "Apple", "Kiwi"); 
    $sorted_array = array_diff($inputArray,$order); 
    sort($sorted_array); 
    $rest_array = array_intersect($order,$inputArray);  
    $result = array_merge($rest_array,$sorted_array); 
    return $result; 
} 

沒有測試過,但嘗試。

+0

我已經編輯它,所以如果它不起作用,請再試一次。 – Headshota 2011-04-12 08:50:50

1

可能比Headshota的解決方案慢一些,但只是給你提供了另一種(未測試)的可能性:

function sortWordsCmp($a, $b) { 
    $order=array("Banana","Orange", "Apple", "Kiwi"); 
    $a = array_search($a, $order); 
    $b = array_search($b, $order); 

    if ($a === $b) 
    return 0; 

    return (($b===false) || ($a < $b)) ? -1 : +1; 
} 

public static function sortWords($inputArray){ 
    usort($inputArray, 'sortWordsCmp'); 
    return $inputArray; 
} 
12

PHP提供了​​和uksort()功能,讓你寫你自己的排序例程。這兩個中,你會想要usort()

這兩個函數都希望您編寫一個獨立函數,它將輸入數組的兩個元素作爲輸入,並返回它們應該排序的順序。然後usort()函數運行自己的排序算法,調用函數以按照需要經常建立排序順序,直到排序完整的數組。

所以你會寫這樣的事情....

function mycompare($a, $b) { 
    if ($a == $b) {return 0;} 
    $order=array("Banana","Orange", "Apple", "Kiwi"); 
    $position = array_search($a,$order); 
    $position2 = array_search($b, $order); 

    //if both are in the $order, then sort according to their order in $order... 
    if ($position2!==false && $position!==false) {return ($position < $position2) ? -1 : 1;} 
    //if only one is in $order, then sort to put the one in $order first... 
    if($position!==false) {return -1;} 
    if($position2!==false) {return 1;} 

    //if neither in $order, then a simple alphabetic sort... 
    return ($a < $b) ? -1 : 1; 
} 

...然後就打電話usort($inputarray,'mycompare');對它們進行排序。

0
public static function sortByArbitraryKeys(&$inputArray, $sort_order) { 
    $sort_order = array_flip($sort_order); 
    uksort($inputArray, function ($a, $b) use ($sort_order) { 
     return $sort_order[$a] - $sort_order[$b]; 
    } 
} 

因此,一個例子是以下...

// Doe, John L. 
$this->full_name = ['last_name'=>'Doe', 'first_name'=>'John', 'middle_initial'=>'L.']; 

// John L. Doe 
$this->sortByArbitraryKeys($this->full_name, ['first_name', 'middle_initial', 'last_name']); 

您可以輕鬆地重構本作無論你的具體使用情況。

+0

'$ sort_array'是什麼? – crmpicco 2015-11-12 14:05:14

+0

哦,那是我搞砸了。 – kjg61pt 2015-11-14 03:20:17