English 中文(简体)
分布式阵列在数字上[复制]
原标题:Sorting arrays numerically [duplicate]

我需要分类一个PHP阵列。 我列举了以下实例。 我需要把最高级的10个“URLS”和他们的视角放在不同的阵列中。 我知道,如果只有10位顶端,我会遇到问题......如果发生这种情况,那么随机对等就会被罚款。

任何建议?

我曾尝试过某种(神秘的)但这种说法只是一阵列中的第一个物体,需要打第二。

任何想法?


Array
(
    [0] => Array
        (
            [name] => http://bit.ly/2oUTzf
            [count] => 1
        )

    [1] => Array
        (
            [name] => http://tiny.cc/wyNbi
            [count] => 1
        )

    [2] => Array
        (
            [name] => http://ow.ly/Almo
            [count] => 1
        )

    [3] => Array
        (
            [name] => http://bit.ly/7bQ8sY
            [count] => 1
        )

    [4] => Array
        (
            [name] => http://kissa.be/w4V-
            [count] => 5
        )

    [5] => Array
        (
            [name] => http://ow.ly/xzwI
            [count] => 1
        )

    [6] => Array
        (
            [name] => http://twa.lk/L6FZX
            [count] => 1
        )

    [7] => Array
        (
            [name] => http://tinyurl.com/Alyssa10
            [count] => 1
        )

    [8] => Array
        (
            [name] => http://www.hiderefer.com/0cz7kNgA.htm
            [count] => 1
        )

    [9] => Array
        (
            [name] => http://tinyurl.com/Joanie515
            [count] => 1
        )

    [10] => Array
        (
            [name] => http://ow.ly/uJvB
            [count] => 1
        )

    [11] => Array
        (
            [name] => http://tinyurl.com/
            [count] => 1
        )

    [12] => Array
        (
            [name] => http://www.hiderefer.com/wJBUhh3G.htm
            [count] => 1
        )

    [13] => Array
        (
            [name] => http://short.to/xcxc
            [count] => 1
        )

    [14] => Array
        (
            [name] => http://bit.ly/t79FA
            [count] => 2
        )

    [15] => Array
        (
            [name] => http://tinyurl.com/yzy33yl
            [count] => 1
        )

    [16] => Array
        (
            [name] => http://p.gs/zksz6
            [count] => 1
        )

    [17] => Array
        (
            [name] => http://bit.ly/7E1cc8
            [count] => 1
        )

    [18] => Array
        (
            [name] => http://bit.ly/6hbugu
            [count] => 1
        )

    [19] => Array
        (
            [name] => http://tii.libsyn.com/index.php
            [count] => 6
        )

    [20] => Array
        (
            [name] => http://tinyurl.com/nlzzwq
            [count] => 1
        )

    [21] => Array
        (
            [name] => http://bit.ly/7gAdXi
            [count] => 1
        )

    [22] => Array
        (
            [name] => http://localtweeps.com
            [count] => 1
        )

    [23] => Array
        (
            [name] => http://localtweeps.com.
            [count] => 3
        )

    [24] => Array
        (
            [name] => http://scribd.com/doc/22365778
            [count] => 1
        )

    [25] => Array
        (
            [name] => http://quick-weight-loss-secrets.com/
            [count] => 1
        )

    [26] => Array
        (
            [name] => http://tinyurl.com/ykd5qm5
            [count] => 1
        )

    [27] => Array
        (
            [name] => http://bit.ly/5DQ6SO
            [count] => 1
        )

    [28] => Array
        (
            [name] => http://bit.ly/4z6Kww
            [count] => 1
        )

    [29] => Array
        (
            [name] => http://bit.ly/40sm9N
            [count] => 1
        )

    [30] => Array
        (
            [name] => http://bit.ly/8mh7DO
            [count] => 5
        )

    [31] => Array
        (
            [name] => http://tinyurl.com/krt5yf
            [count] => 1
        )

    [32] => Array
        (
            [name] => http://bit.ly/7GsthV
            [count] => 1
        )

    [33] => Array
        (
            [name] => http://bit.ly/1QJzvM
            [count] => 1
        )

    [34] => Array
        (
            [name] => http://yfrog.com/1durkj
            [count] => 1
        )

    [35] => Array
        (
            [name] => http://budurl.com/dxwc
            [count] => 9
        )

    [36] => Array
        (
            [name] => http://digg.com/d1qiCr
            [count] => 1
        )

    [37] => Array
        (
            [name] => http://bit.ly/eVSIo
            [count] => 1
        )

    [38] => Array
        (
            [name] => http://yfrog.com/37badgj
            [count] => 2
        )

    [39] => Array
        (
            [name] => http://tinyurl.com/qh8sos
            [count] => 1
        )

    [40] => Array
        (
            [name] => http://tinyurl.com/mz7l8d
            [count] => 3
        )

    [41] => Array
        (
            [name] => http://tinyurl.com/nratac
            [count] => 1
        )

    [42] => Array
        (
            [name] => http://tinyurl.com/yk587jx
            [count] => 1
        )

    [43] => Array
        (
            [name] => http://www.bethel.edu/alumni/homecoming/09/events/
            [count] => 1
        )

    [44] => Array
        (
            [name] => http://www.waytofit.net
            [count] => 1
        )

    [45] => Array
        (
            [name] => http://twitpic.com/rdcy8
            [count] => 1
        )

    [46] => Array
        (
            [name] => http://retwt.me/1C1Vd
            [count] => 14
        )

    [47] => Array
        (
            [name] => http://www.starbucks.com/card
            [count] => 1
        )

    [48] => Array
        (
            [name] => http://tinyurl.com/yhkbfqe
            [count] => 13
        )

    [49] => Array
        (
            [name] => http://bit.ly/playspy
            [count] => 1
        )

    [50] => Array
        (
            [name] => http://bit.ly/57rHLO
            [count] => 12
        )
最佳回答

您需要撰写一种习俗分类功能,如:

function MyCustomSort($a, $b)
{
    if ($a->count == $b->count) {
        return 0;
    }
    return ($a->count < $b->count) ? -1 : 1;
}

接着,你将这一职能变成了一种方式——如:

usort($myArray, "MyCustomSort");

您也可以撰写一份帮助您通过网站进行分类的职能,如:

function MyCustomSort($a, $b)
{
    if ($a->name == $b->name) {
        return 0;
    }
    return ($a->name < $b->name) ? -1 : 1;
}
问题回答

您可使用decorate-sort-undecorate

<?php

$arr = Array
(
    [0] => Array
        (
            [name] => http://bit.ly/2oUTzf
            [count] => 1
        )

    [1] => Array
        (
            [name] => http://tiny.cc/wyNbi
            [count] => 1
        )

    [2] => Array
        (
            [name] => http://ow.ly/Almo
            [count] => 1
        )

    [3] => Array
        (
            [name] => http://bit.ly/7bQ8sY
            [count] => 1
        )
...
);

// actual sorting below
$arr= array_map(create_function( $a ,  return array($a["count"], $a); ), $arr); // transform into array of arrays consisted of sort key and item
sort($arr); // sort array of arrays
$arr = array_map( end , $arr); // take only last element from each array

print_r($arr);

How does it work?

不要把你的许多项目分类,而是你想要分类的几套阵列,其最后一个要素是项目,首先是关键。 在对你进行分类后,你只保留该项目。

你们可以仅使用<代码>sort,对阵列进行分类,因为PHP对两个相同的长度阵列进行了比较,将其内容逐一比较。

Sorting by multiple fields

如果算法相同,如果算法相同,则你可以使用一种以上的钥匙。 你们可以这样做,把多个关键因素同重要秩序联系起来。

$arr = array_map(create_function( $a ,  return array($a["count"], $a["name"], $a); ), $arr); 

Why it s fast

采用<代码>usort的方式可能更快 因为它把你的习俗规律仅仅称为一小时,以划分一系列长度。 比较时使用比较器进行的比较应当迅速。 在<条码>中 方法习惯比较器在分类期间被多次称为(超过零倍),可能会放慢。

function sortByCount($a, $b)
{
    if ($a[ count ] == $b[ count ]) {
        return 0;
    }
    return ($a[ count ] < $b[ count ]) ? -1 : 1;
}

usort($myarray, "sortByCount");




相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...

热门标签