How to sort several arrays

Source: Internet
Author: User

1. Sorting a key value for an array

functionArray_sort ($arr,$keys,$type= ' ASC '){    if(Empty($arr))        return $arr; $keysvale=$newq _array=Array(); foreach($arr  as $k=$v){        $keysvale[$k] =$v[$keys]; }    if($type= = ' ASC '){        Asort($keysvale); }Else{        Arsort($keysvale); }    Reset($keysvale); foreach($keysvale  as $k=$v){        $newq _array[$k] =$arr[$k]; }    return $newq _array;}

2. Bubble sort based on array string length

//Array Bubble SortfunctionBubble_sort_by_length ($array){    $count=Count($array); if($count<= 0){        return $array; }     for($i= 0;$i<$count;$i++){         for($k=$count-1;$k>$i;$k--){            if(strlen($array[$k]) >strlen($array[$k-1])){                $tmp=$array[$k]; $array[$k] =$array[$k-1]; $array[$k-1] =$tmp; }        }    }    return $array;}

3. Bubble sort based on two keys inside the array

//Array Bubble SortfunctionBubble_sort_by_key ($array,$key,$key _2,$type= ' ASC ',$type _2= ' ASC '){    $count=Count($array); if($count<= 0 | |Empty($array)){        return Array(); }    if($type= = ' ASC ') {         for($i= 0;$i<$count;$i++) {             for($k=$count-1;$k>$i;$k--) {                if($array[$k][$key] <$array[$k-1][$key]) {                    $tmp=$array[$k]; $array[$k] =$array[$k-1]; $array[$k-1] =$tmp; }                if($array[$k][$key] ==$array[$k-1][$key]) {                    if($type _2= = ' ASC ') {                        if($array[$k][$key _2] <$array[$k-1][$key _2]) {                            $tmp=$array[$k]; $array[$k] =$array[$k-1]; $array[$k-1] =$tmp; }                    }Else{                        if($array[$k][$key _2] >$array[$k-1][$key _2]) {                            $tmp=$array[$k]; $array[$k] =$array[$k-1]; $array[$k-1] =$tmp; }                    }                }            }        }    }Else{         for($i= 0;$i<$count;$i++) {             for($k=$count-1;$k>$i;$k--) {                if($array[$k][$key] >$array[$k-1][$key]) {                    $tmp=$array[$k]; $array[$k] =$array[$k-1]; $array[$k-1] =$tmp; }                if($array[$k][$key] ==$array[$k-1][$key]) {                    if($type _2= = ' ASC ') {                        if($array[$k][$key _2] <$array[$k-1][$key _2]) {                            $tmp=$array[$k]; $array[$k] =$array[$k-1]; $array[$k-1] =$tmp; }                    }Else{                        if($array[$k][$key _2] >$array[$k-1][$key _2]) {                            $tmp=$array[$k]; $array[$k] =$array[$k-1]; $array[$k-1] =$tmp; }                    }                }            }        }    }    return $array;}

How to sort several arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.