2011-04-12 64 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